Skip to content

Commit cf1a431

Browse files
committed
WIP: metaevent in demo 3
1 parent afa7216 commit cf1a431

35 files changed

+964
-203
lines changed

.vscode/launch.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Listen for Xdebug",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9003
12+
},
13+
{
14+
"name": "Launch currently open script",
15+
"type": "php",
16+
"request": "launch",
17+
"program": "${file}",
18+
"cwd": "${fileDirname}",
19+
"port": 0,
20+
"runtimeArgs": [
21+
"-dxdebug.start_with_request=yes"
22+
],
23+
"env": {
24+
"XDEBUG_MODE": "debug,develop",
25+
"XDEBUG_CONFIG": "client_port=${port}"
26+
}
27+
},
28+
{
29+
"name": "Launch Built-in web server",
30+
"type": "php",
31+
"request": "launch",
32+
"runtimeArgs": [
33+
"-dxdebug.mode=debug",
34+
"-dxdebug.start_with_request=yes",
35+
"-S",
36+
"http://www.thruway_examples.local/metaevents.php:0"
37+
],
38+
"program": "",
39+
"cwd": "${workspaceRoot}",
40+
"port": 9003,
41+
"serverReadyAction": {
42+
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
43+
"uriFormat": "http://www.thruway_examples.local/metaevents.php:%s",
44+
"action": "openExternally"
45+
}
46+
}
47+
]
48+
}

Demo/InternalClient.php DEMO1/InternalClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Demo;
3+
namespace DEMO1;
44

55
use MockModel\MyMockModel;
66
// use MyApp\Models\MyModel;

Demo/InternalClient2.php DEMO1/InternalClient2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Demo;
3+
namespace DEMO1;
44

55
/**
66
* this is the internal client 2, this class should be used

Demo/SimpleAuthProviderClient.php DEMO1/SimpleAuthProviderClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Demo;
3+
namespace DEMO1;
44

55
/**
66
* Class SimpleAuthProviderClient

Demo/SimpleClientAuth.php DEMO1/SimpleClientAuth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Demo;
3+
namespace DEMO1;
44

55
use Thruway\Message\ChallengeMessage;
66

bin/launchInternalClient.php DEMO1/bin/launchInternalClient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
3+
require __DIR__ . '/../../vendor/autoload.php';
44

5-
use Demo\InternalClient;
6-
use Demo\SimpleClientAuth;
5+
use DEMO1\InternalClient;
6+
use DEMO1\SimpleClientAuth;
77

88
$client = new InternalClient('somerealm');
99

bin/launchInternalClient2.php DEMO1/bin/launchInternalClient2.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
3+
require __DIR__ . '/../../vendor/autoload.php';
44

5-
use Demo\InternalClient2;
6-
use Demo\SimpleClientAuth;
5+
use DEMO1\InternalClient2;
6+
use DEMO1\SimpleClientAuth;
77

88
$client2 = new InternalClient2('somerealm');
99

bin/launchInternalClientAsClientObject.php DEMO1/bin/launchInternalClientAsClientObject.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
3+
require __DIR__ . '/../../vendor/autoload.php';
44

55
use Thruway\ClientSession;
66
use Thruway\Peer\Client as ThruwayPeerClient;
77
use Thruway\Transport\PawlTransportProvider;
8-
use Demo\SimpleClientAuth;
8+
use DEMO1\SimpleClientAuth;
99

1010
/**
1111
* this acts like an internal client 2 using a procedural style

bin/launchInternalClientAsConnectionObject.php DEMO1/bin/launchInternalClientAsConnectionObject.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
3+
require __DIR__ . '/../../vendor/autoload.php';
44

55
use Thruway\ClientSession;
66
use Thruway\Connection;
@@ -50,9 +50,7 @@
5050

5151
/**
5252
* please note that if you want to keep the client connected
53-
* you have to delete $connection->close()
54-
*
55-
* this is not the case, since we want to execute this code to make the RPC call and obtain the response.
53+
* you have to delete $connection->close(). This is not the case, since we want to execute this code to make the RPC call and obtain the response.
5654
* An example scenario would be to make an XHR request from the browser using something like jquery ajax, then
5755
* a file containing this code will act as a client connecting to the router and make the RPC call, obtaining
5856
* whaterver data you need, then disconnect and return the obtained data. If you delete $connection->close()

bin/SimpleWsRouter.php DEMO1/bin/router.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

3-
require_once __DIR__ . "/../vendor/autoload.php";
3+
require_once __DIR__ . "/../../vendor/autoload.php";
44

55
use Thruway\Peer\Router;
66
use Thruway\Transport\RatchetTransportProvider;
7-
use Demo\SimpleAuthProviderClient;
7+
use DEMO1\SimpleAuthProviderClient;
8+
9+
10+
/**
11+
* this script is based on:
12+
* https://github.com/voryx/Thruway/blob/master/Examples/Authentication/StartRouter.php
13+
*
14+
*/
815

916
$router = new Router();
10-
$loop = $router->getLoop(); //so far not used
1117

1218
$router->registerModule(new \Thruway\Authentication\AuthenticationManager());
1319

DEMO1/readme.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# DEMO 1:
2+
3+
##### Purpose: have a router, have a realm with authentication, internal client registering RPC methods, call the RPC methos usin internal client 2 and browser client.
4+
5+
**This demo consists of:**
6+
7+
* Router: based on Examples/SimpleWsRouter.php
8+
* Realm Authentication: based on Examples/Authentication/SimpleAuthProviderClient.php
9+
* Internal Client 1: based on Examples/ClientClass/FreeSpaceClient.php
10+
* Autobahn client: based on the autobahn-js [repo](https://github.com/crossbario/autobahn-js) and Examples/Authentication/simpleAutobahnAuth.html
11+
* Internal Client 2 (connection object): based on Examples/SimpleClient.php
12+
13+
14+
### How to use demo 1
15+
16+
Open 3 terminals and a browser tab with console in js option (F12 in Chrome, select js tab)
17+
18+
navigate to root dir, at the same level of composer.json,
19+
so if you do ls/dir command, you should see composer.json
20+
21+
**in terminal 1:**
22+
23+
> php DEMO1/bin/router.php
24+
25+
This will launch the router, add auth manager and auth client, and add transport provider.
26+
27+
28+
**in terminal 2:**
29+
30+
> php DEMO1/bin/launchInternalClient.php
31+
32+
This will start the internal client using an instance of the InternalClient.php, using a client authenticator to enter the realm.
33+
34+
35+
**in terminal 3**, choose one of the following, but try them all at least once:
36+
37+
All of these act like a second internal client. the idea is that internal client 2 can be used by the main app to interface with the WAMP server.
38+
39+
40+
**option 1**, using use Thruway\Connection object:
41+
42+
> php DEMO1/bin/launchInternalClientAsConnectionObject.php
43+
44+
45+
**option 2**, using Thruway\Peer\Client object:
46+
47+
> php DEMO1/bin/launchInternalClientAsClientObject.php
48+
49+
50+
**option 3** using Thruway\Peer\Client subclass:
51+
52+
> php bin/launchInternalCLient2.php
53+
54+
**in browser:** in file index.php in a script tag there is a function defined, in the console in JS tab, call the function
55+
> runConnectionObjectUsingAjax();

Demo/AuthProviderClient.php DEMO2/AuthProviderClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Demo;
3+
namespace DEMO2;
44

55
use Thruway\Logging\Logger;
66

DEMO2/InternalClient.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace DEMO2;
4+
5+
use MockModel\MyMockModel;
6+
// use MyApp\Models\MyModel;
7+
// use MyApp\Mailer\MyMailer;
8+
9+
/**
10+
* this is the internal client 1
11+
*
12+
* here you should create methods that interface with your main app or query the loop object
13+
*/
14+
class InternalClient extends \Thruway\Peer\Client
15+
{
16+
17+
/**
18+
* @return array
19+
*/
20+
public function getFreeSpace()
21+
{
22+
return ["Free space: " . (string)disk_free_space('/')]; // wsl or linux
23+
// return [disk_free_space('c:')]; // use c: for you windowers
24+
}
25+
26+
/**
27+
* Example methods interfacing with your main app
28+
*/
29+
// public function sendMail($args): array
30+
// {
31+
// $someData = MyModel::getSomeData($args);
32+
// return MyMailer::sendMailUsingThisData($someData);
33+
// }
34+
// public function getSomeDataFromApp(): array
35+
// {
36+
// $someData = MyModel::getUsefulData();
37+
// return $someData;
38+
// }
39+
40+
/**
41+
* example returning mock data
42+
*/
43+
public function getMockData(): array
44+
{
45+
return MyMockModel::getMyMockData();
46+
}
47+
48+
/**
49+
* @param \Thruway\ClientSession $session
50+
* @param \Thruway\Transport\TransportInterface $transport
51+
*/
52+
public function onSessionStart($session, $transport)
53+
{
54+
$session->register('com.example.getfreespace', [$this, 'getFreeSpace']);
55+
$session->register('com.example.getMockData', [$this, 'getMockData']);
56+
// $session->register('com.example.sendMail', [$this, 'sendMail']); //RPC example
57+
// $session->register('com.example.getSomeDataFromApp', [$this, 'getSomeDataFromApp']); //RPC example
58+
}
59+
60+
}
61+

0 commit comments

Comments
 (0)