forked from Devristo/phpws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_echo.php
39 lines (31 loc) · 1.38 KB
/
ssl_echo.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
#!/php -q
<?php
require_once("../vendor/autoload.php");
use Devristo\Phpws\Messaging\WebSocketMessageInterface;
use Devristo\Phpws\Protocol\WebSocketTransportInterface;
use Devristo\Phpws\Server\WebSocketServer;
$loop = \React\EventLoop\Factory::create();
// Create a logger which writes everything to the STDOUT
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);
// Create a WebSocket server using SSL
$server = new WebSocketServer("ssl://0.0.0.0:12345", $loop, $logger);
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'local_cert', "democert.pem");
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
$server->setStreamContext($context);
// Sent a welcome message when a client connects
$server->on("connect", function(WebSocketTransportInterface $user){
$user->sendString("Hey! I am the echo robot. I will repeat all your input!");
});
// Echo back any message the user sends
$server->on("message", function(WebSocketTransportInterface $user, WebSocketMessageInterface $message) use($logger){
$logger->notice(sprintf("We have got '%s' from client %s", $message->getData(), $user->getId()));
$user->sendString($message->getData());
});
// Bind the server
$server->bind();
// Start the event loop
$loop->run();