Skip to content

Commit c544e1a

Browse files
committed
Feature: demo 6, reverse proxy for secure web socket connection
1 parent bd25a65 commit c544e1a

20 files changed

+2017
-35
lines changed

DEMO6/MockMsg.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DEMO6;
6+
7+
class MockMsg{
8+
9+
private static $greetings = [
10+
"Hello",
11+
"How do you do",
12+
"it's been a while",
13+
"how're you doing",
14+
"Nice to meet you",
15+
"how's it going?",
16+
"what's up",
17+
"Hi",
18+
"Good morning",
19+
"Good to see you again",
20+
"hahahaha",
21+
"What time is it?",
22+
"I'm tired!!!",
23+
"what about Joe?",
24+
"is anyone interested in gardening?",
25+
"Nahhh",
26+
"I'm off",
27+
"I'm hungry, who is up for McDonalds??",
28+
"lets go get some McDonalds!!!!",
29+
"I've got a lot to do",
30+
"hey, you again?",
31+
"I thought it was just me!",
32+
"hooooorayyy!!",
33+
"I need to go feed my pets, bye",
34+
"how many cats do you have",
35+
"I don't have any cats, just dogs",
36+
"but how many then",
37+
"four, all small",
38+
"wow, thats a lot",
39+
"it gets worse and worse by the minute",
40+
"are you doing something this weekend?",
41+
"I want to go to the movies",
42+
"I rather just stay at home",
43+
"well, that escalated quickly",
44+
"hola, mucho gusto",
45+
"do you speak spanish?",
46+
"na, just know enough to say hello hahahah",
47+
"Hey I saw you the last night, you were buyng a burrito!",
48+
"mmmm, last time I went out for a burrito was 2 weeks ago, so I think it wasn't me, sorry",
49+
"oh",
50+
"dammit, now I'm thinking about getting a burrito!",
51+
"ok, lets go to that burrito joint near the train station, see you there in 15",
52+
"ahhh damn, no, sorry, it will have to be in 40, I need to get the washing machine going and then I can go out",
53+
];
54+
55+
public static function getRandomMockGreeting(): string
56+
{
57+
$key = array_rand(self::$greetings);
58+
return self::$greetings[$key];
59+
}
60+
61+
public static function getGreetingsArray(): array
62+
{
63+
return self::$greetings;
64+
}
65+
}

DEMO6/bin/otherInternalClient.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Rx\Thruway\Client;
4+
use DEMO6\MockMsg;
5+
use Thruway\Message\ResultMessage;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
/**
10+
* This client is intended to produce trafic by publishing random content
11+
* to the "demo6" topic every 5 seconds
12+
*/
13+
14+
$theRealm = [
15+
"name" => "somerealm",
16+
"signature" => "letMeIn"
17+
];
18+
19+
$client = new Client('ws://127.0.0.1:9090', $theRealm['name'], ['authmethods' => ['simplysimple']]);
20+
21+
$client->onChallenge(function() use ($theRealm) {
22+
return $theRealm['signature'];
23+
});
24+
25+
$source = \Rx\Observable::interval(5000)->do(function () use ($client){
26+
$client->publish('demo6', json_encode(MockMsg::getRandomMockGreeting()));
27+
});
28+
29+
// subscribe
30+
$client
31+
->topic('demo6')
32+
->subscribe();
33+
34+
// publish
35+
$client->publish('demo6', $source->subscribe());

DEMO6/bin/router.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
require __DIR__ . "/../../vendor/autoload.php";
5+
6+
use Thruway\Peer\Router;
7+
use Thruway\Transport\RatchetTransportProvider;
8+
9+
$theRealm = [
10+
"name" => "somerealm",
11+
"signature" => "letMeIn"
12+
];
13+
14+
$router = new Router();
15+
$loop = $router->getLoop();
16+
17+
// lets use the default transport
18+
$transportProvider = new RatchetTransportProvider("127.0.0.1", 9090);
19+
20+
$router->addTransportProvider($transportProvider);
21+
22+
// lets use the demo3 auth provider client in order not to bloat the demo with duplicate files
23+
$realmAuthProvider = new \DEMO3\AuthProviderClient([$theRealm["name"]], $loop);
24+
$realmAuthProvider->setSignature($theRealm["signature"]);
25+
26+
/** adds auth provider client in router */
27+
$router->addInternalClient($realmAuthProvider);
28+
29+
/** registers the authentication module so it requires the auth provider client */
30+
$router->registerModule(new \Thruway\Authentication\AuthenticationManager());
31+
32+
// we will need the loop object to create the periodic publishing to the topic "demo6"
33+
$router->addInternalClient(new \DEMO6\InternalClient6($theRealm["name"], $loop));
34+
35+
$router->start();

DEMO6/internalClient6.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace DEMO6;
4+
5+
use Thruway\Logging\Logger;
6+
7+
/**
8+
* Class InternalClient6 based on Examples/MetaEvent/InternalClient.php
9+
*
10+
*
11+
*/
12+
class InternalClient6 extends \Thruway\Peer\Client
13+
{
14+
/**
15+
* List sessions info
16+
*
17+
* @var array
18+
*/
19+
protected $_sessions = [];
20+
21+
/**
22+
* Constructor
23+
*/
24+
public function __construct($realm_name, \React\EventLoop\LoopInterface $loop)
25+
{
26+
parent::__construct($realm_name, $loop);
27+
}
28+
29+
/**
30+
* @param \Thruway\ClientSession $session
31+
* @param \Thruway\Transport\TransportInterface $transport
32+
*/
33+
public function onSessionStart($session, $transport)
34+
{
35+
Logger::debug($this, "--------------- Hello from InternalClient ------------");
36+
Logger::debug($this, "registering getphpversion");
37+
38+
$session->register('getphpversion', [$this, 'getPhpVersion']);
39+
}
40+
41+
/**
42+
* Handle get PHP version
43+
*
44+
* @return array
45+
*/
46+
public function getPhpVersion()
47+
{
48+
return [phpversion()];
49+
}
50+
51+
52+
}

0 commit comments

Comments
 (0)