-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.php
More file actions
41 lines (33 loc) · 1.02 KB
/
socket_server.php
File metadata and controls
41 lines (33 loc) · 1.02 KB
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
<?php
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Server\IoServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class WebSocketServer implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
echo "Message received: {$msg}\n";
$from->send("Server response: {$msg}");
}
public function onClose(ConnectionInterface $conn) {
echo "Connection closed ({$conn->resourceId})\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error occurred: {$e->getMessage()}\n";
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new WebSocketServer()
)
),
8080
);
echo "WebSocket server running at ws://127.0.0.1:8080\n";
$server->run();