Skip to content

Commit a2d4676

Browse files
committed
Time example
1 parent 9cd38b2 commit a2d4676

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

examples/time.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>WebSocket TEST</title>
4+
</head>
5+
<body>
6+
<h1>Server Time</h1>
7+
<strong id="time"></strong>
8+
9+
<script>
10+
var socket = new WebSocket("ws://localhost:12345/");
11+
socket.onmessage = function(msg) {
12+
document.getElementById("time").innerText = msg.data;
13+
};
14+
</script>
15+
</body>
16+
</html>

examples/time.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/php -q
2+
<?php
3+
require_once("../vendor/autoload.php");
4+
use Devristo\Phpws\Server\WebSocketServer;
5+
6+
$loop = \React\EventLoop\Factory::create();
7+
8+
// Create a logger which writes everything to the STDOUT
9+
$logger = new \Zend\Log\Logger();
10+
$writer = new Zend\Log\Writer\Stream("php://output");
11+
$logger->addWriter($writer);
12+
13+
// Create a WebSocket server using SSL
14+
$server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
15+
16+
$loop->addPeriodicTimer(0.5, function() use($server, $logger){
17+
$time = new DateTime();
18+
$string = $time->format("Y-m-d H:i:s");
19+
$logger->notice("Broadcasting time to all clients: $string");
20+
foreach($server->getConnections() as $client)
21+
$client->sendString($string);
22+
});
23+
24+
25+
// Bind the server
26+
$server->bind();
27+
28+
// Start the event loop
29+
$loop->run();

0 commit comments

Comments
 (0)