This repository has been archived by the owner on Sep 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
/
demo_ssl.php
109 lines (81 loc) · 3.11 KB
/
demo_ssl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/php -q
<?php
require_once("vendor/autoload.php");
// Run from command prompt > php demo.php
use Devristo\Phpws\Framing\WebSocketFrame;
use Devristo\Phpws\Framing\WebSocketOpcode;
use Devristo\Phpws\Messaging\IWebSocketMessage;
use Devristo\Phpws\Protocol\IWebSocketConnection;
use Devristo\Phpws\Server\IWebSocketServerObserver;
use Devristo\Phpws\Server\UriHandler\WebSocketUriHandler;
use Devristo\Phpws\Server\WebSocketServer;
/**
* This demo resource handler will respond to all messages sent to /echo/ on the socketserver below
*
* All this handler does is echoing the responds to the user
* @author Chris
*
*/
class DemoSslEchoHandler extends WebSocketUriHandler {
public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->logger->notice("[ECHO] {$msg->getData()}");
// Echo
$user->sendMessage($msg);
}
public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj) {
$this->logger->notice("[DEMO] Admin TEST received!");
$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}
}
/**
* Demo socket server. Implements the basic eventlisteners and attaches a resource handler for /echo/ urls.
*
*
* @author Chris
*
*/
class DemoSslSocketServer implements IWebSocketServerObserver {
protected $debug = true;
protected $server;
public function __construct() {
$logger = new \Zend\Log\Logger();
$logger->addWriter(new Zend\Log\Writer\Stream("php://output"));
$this->logger = $logger;
$this->server = new WebSocketServer("ssl://0.0.0.0:12345", $logger);
$this->server->addObserver($this);
$this->server->addUriHandler("echo", new DemoSslEchoHandler($logger));
$this->setupSSL();
}
private function getPEMFilename() {
return './democert.pem';
}
public function setupSSL() {
$context = stream_context_create();
// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', $this->getPEMFilename());
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
$this->server->setStreamContext($context);
}
public function onConnect(IWebSocketConnection $user) {
$this->logger->notice("[DEMO] {$user->getId()} connected");
}
public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->logger->notice("[DEMO] {$user->getId()} says '{$msg->getData()}'");
}
public function onDisconnect(IWebSocketConnection $user) {
$this->logger->notice("[DEMO] {$user->getId()} disconnected");
}
public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->logger->notice("[DEMO] Admin Message received!");
$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}
public function run() {
$this->server->run();
}
}
// Start server
$server = new DemoSslSocketServer();
$server->run();