Skip to content

Commit 1720671

Browse files
committed
Improvement: DEMO3 finished
1 parent cf1a431 commit 1720671

6 files changed

+84
-18
lines changed

DEMO3/InternalClient3.php

+34-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DEMO3;
44

55
use MockModel\MyMockModel;
6+
use Thruway\Logging\Logger;
67

78
/**
89
* Class InternalClient3 based on Examples/MetaEvent/InternalClient.php
@@ -32,7 +33,16 @@ public function __construct()
3233
*/
3334
public function onSessionStart($session, $transport)
3435
{
35-
echo "--------------- Hello from InternalClient ------------\n";
36+
Logger::debug($this, "--------------- Hello from InternalClient ------------");
37+
Logger::debug($this, "registering getphpversion");
38+
Logger::debug($this, "registering getonline");
39+
Logger::debug($this, "registering getfreespace");
40+
Logger::debug($this, "registering getMockData");
41+
Logger::debug($this, "registering isTheUserConnected");
42+
Logger::debug($this, "registering ws_login");
43+
Logger::debug($this, "listenint for wamp.metaevent.session.on_join events");
44+
Logger::debug($this, "listening for wamp.metaevent.session.on_leave events");
45+
3646
$session->register('com.example.getphpversion', [$this, 'getPhpVersion']);
3747
$session->register('com.example.getonline', [$this, 'getOnline']);
3848
$session->register('com.example.getfreespace', [$this, 'getFreeSpace']);
@@ -59,6 +69,7 @@ public function getPhpVersion()
5969
*/
6070
public function getFreeSpace()
6171
{
72+
Logger::debug($this, "internal client: managing RPC getFreeSpace");
6273
return ["Free space: " . (string)disk_free_space('/')];
6374
// return [disk_free_space('c:')]; // use c: for you windowers
6475
}
@@ -68,6 +79,7 @@ public function getFreeSpace()
6879
*/
6980
public function getMockData(): array
7081
{
82+
Logger::debug($this, "internal client: managing RPC getMockData");
7183
return MyMockModel::getMyMockData();
7284
}
7385

@@ -78,6 +90,7 @@ public function getMockData(): array
7890
*/
7991
public function getOnline()
8092
{
93+
Logger::debug($this, "internal client: managing RPC getOnline");
8194
return [$this->_sessions];
8295
}
8396

@@ -110,18 +123,26 @@ public function isTheUserConnected($args)
110123
*/
111124
public function ws_login($args)
112125
{
126+
Logger::debug($this, "internal client: managing RPC ws_login, args: " . json_encode($args));
113127
$marked = false;
128+
$array_args = self::toArray($args[0]);
114129
foreach($this->_sessions as $ws_id => $ws_data){
115-
if($ws_id == $args[1]){
116-
$this->_sessions[$ws_id]['user_id'] = $args[0];
130+
if($ws_id == $array_args['ws_session_id']){
131+
$this->_sessions[$ws_id]['user_id'] = $array_args['user_id'];
132+
$this->_sessions[$ws_id]['user_data'] = MyMockModel::getUserById($array_args['user_id']);
117133
$marked = true;
118134
break;
119135
}
120136
}
121137

122138
if($marked){
123139
return [
124-
"ws_session_data" => $this->_sessions[$ws_id]
140+
"ws_session_data" => $this->_sessions[$ws_id],
141+
"callback" => [
142+
"is_rpc" => true,
143+
"method" => "com.example.getonline",
144+
//"args" => [],
145+
],
125146
];
126147
}else{
127148
// something went wrong
@@ -143,11 +164,12 @@ public function ws_login($args)
143164
*/
144165
public function onSessionJoin($args, $kwArgs, $options)
145166
{
167+
Logger::debug($this, "internal client: event onSessionJoin");
146168
$data = self::toArray($args);
147169
$ws_session_id = $data[0]["session"];
148170
$realm = $data[0]["realm"];
149171

150-
echo "Session {$data[0]['session']} joinned\n";
172+
Logger::debug($this, "Session {$data[0]['session']} joinned");
151173
$this->_sessions[$ws_session_id] = [
152174
"ws_session_id" => $ws_session_id,
153175
"realm" => $realm,
@@ -166,21 +188,25 @@ public function onSessionJoin($args, $kwArgs, $options)
166188
*/
167189
public function onSessionLeave($args, $kwArgs, $options)
168190
{
191+
Logger::debug($this, "internal client: event onSessionLeave");
169192
$data = self::toArray($args);
170193
$ws_session_id = $data[0]["session"];
171194

172195
if (!empty($ws_session_id)) {
173196

174-
foreach($this->_sessions as $ws_session_id => $ws_session_data){
175-
if($ws_session_id == $ws_session_data["ws_session_id"]){
176-
echo "Session {$ws_session_id} leaved\n";
197+
foreach($this->_sessions as $_session_id => $_session_data){
198+
if($ws_session_id == $_session_id){
199+
Logger::debug($this, "Session {$ws_session_id} leaved");
177200
unset ($this->_sessions[$ws_session_id]);
178201
return;
179202
}
180203
}
181204
}
182205
}
183206

207+
/**
208+
* @param object|string $object
209+
*/
184210
private static function toArray($object)
185211
{
186212
return json_decode(json_encode($object), true);
File renamed without changes.

MockModel/MyMockModel.php

+10
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public static function showUsers(): array
4343
return self::$myMockUsersDB;
4444
}
4545

46+
public static function getUserById(int $user_id): array|false
47+
{
48+
foreach(self::$myMockUsersDB as $row){
49+
if($row['user_id'] == $user_id){
50+
return $row;
51+
}
52+
}
53+
return false;
54+
}
55+
4656
}

js/simpleBrowserClient.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ connection = new autobahn.Connection({
2727
connection.onopen = function(session){
2828
console.log("Successfully made the socket connection.", session._id);
2929

30-
// my_session = session;
30+
my_session = session;
3131

3232
makeRPC('com.example.getphpversion', []);
3333
makeRPC('com.example.getonline', []);
3434
makeRPC('com.example.getfreespace', []);
3535
makeRPC('com.example.getMockData', []);
36-
makeRPC('com.example.ws_login', [user_id, session._id]);
36+
/**
37+
* RPC can take args as array list, but cannot have associative arrays
38+
*/
39+
// makeRPC('com.example.ws_login', [user_id, session._id]); // 2 items in agrs
40+
/**
41+
* to solve this you can pass a json string as an argument item
42+
*/
43+
makeRPC('com.example.ws_login', [{'user_id': user_id, 'ws_session_id': session._id}]); // 1 item in args
3744
};
3845

3946
connection.onclose = function (reason, details) {
@@ -47,20 +54,42 @@ connection.onclose = function (reason, details) {
4754
* @param {array|null} rpc_args
4855
*/
4956
function makeRPC(rpc_method, rpc_args){
50-
// my_session.call(rpc_method, rpc_args).then(
51-
connection.session.call(rpc_method, rpc_args).then(
57+
if(rpc_args != null){
58+
console.log("making RPC: " + rpc_method + " using args: " + JSON.stringify(rpc_args));
59+
}else{
60+
console.log("making RPC: " + rpc_method);
61+
}
62+
my_session.call(rpc_method, rpc_args).then(
63+
// connection.session.call(rpc_method, rpc_args).then(
5264
function(res) {
65+
console.log("Result to " + rpc_method + ":", res);
5366
if(res.hasOwnProperty("callback")){
54-
eval(res.callback);
67+
console.log("RPC " + rpc_method + " has callback");
68+
69+
if(res.callback.is_rpc){
70+
if(res.callback.hasOwnProperty("args")){
71+
makeRPC(res.callback.method, res.callback.args);
72+
}else{
73+
makeRPC(res.callback.method);
74+
}
75+
}else{
76+
if(res.callback.hasOwnProperty("args")){
77+
eval(res.callback.method + "("+ res.callback.args + ")");
78+
}else{
79+
eval(res.callback.method + "()");
80+
}
81+
}
5582
}
56-
return "Result:", res;
83+
// return "Result:", res;
5784
},
5885
function(error) {
59-
return "RPC Call Failure: " + error.error;
86+
// return "RPC Call Failure: " + error.error;
87+
console.log("RPC Call to " + rpc_method + "Failure: " + error.error);
6088
}
6189
);
6290
}
6391

64-
function connect(){
92+
function connect(the_user_id){
93+
user_id = the_user_id;
6594
connection.open();
6695
}

metaevents.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function login(user, password){
4949
// var wamp_host_name = "127.0.0.1";
5050

5151
$.ajax({
52-
url: 'http://' + wamp_host_name + '/Demo/loginController.php',
52+
url: 'http://' + wamp_host_name + '/DEMO3/loginController.php',
5353
type: 'POST',
5454
dataType: 'json',
5555
data: {
@@ -62,6 +62,7 @@ function login(user, password){
6262

6363
console.log('credentials are ok');
6464
user_id = response.user_id;
65+
alert(JSON.stringify(response)); // this alert will interrupt execution so we can see the response, click ok and it will redirect to wellcome (if all is ok)
6566
location.href = 'welcome.php';
6667

6768
}else{

welcome.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script src="js/autobahn.min.js"></script>
3737
<script src="js/simpleBrowserClient.js"></script>
3838
<script>
39-
connect();
39+
connect(<?php echo $_SESSION['id_user'] ?>);
4040
</script>
4141
</body>
4242
</html>

0 commit comments

Comments
 (0)