-
Notifications
You must be signed in to change notification settings - Fork 4
/
websockets.php
120 lines (105 loc) · 4.12 KB
/
websockets.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
110
111
112
113
114
115
116
117
118
119
120
<?php
use Aerys\{ Websocket, Host, Request, Response, Router, function root, function router, function websocket };
/* --- Global server options -------------------------------------------------------------------- */
const AERYS_OPTIONS = [
"keepAliveTimeout" => 60,
//"deflateMinimumLength" => 0,
];
/* --- http://localhost:1337/ ------------------------------------------------------------------- */
class CMS implements Aerys\Websocket {
/**
* Invoked when starting the server
*
* All messages are sent to connected clients by calling methods on the
* Endpoint instance passed in onStart(). Applications must store
* the endpoint instance for use once the server starts.
*
* If the websocket application has external resources it needs to initialize
* (like database connections) this is the place to do it.
*
* If this method is a Generator it will be resolved as a coroutine before
* the server is allowed to start. Additionally, this method returns a
* Promise the server will not start until that promise resolves.
*
* @param \Aerys\Endpoint $endpoint
* @return mixed
*/
public function onStart(Websocket\Endpoint $endpoint) {
print "onStart\n";
}
/**
* Respond to websocket handshake requests
*
* If a websocket application doesn't wish to impose any special constraints on the
* handshake it doesn't have to do anything in this method and all handshakes will
* be automatically accepted.
*
* The return value from onHandshake() invocation (which may be the eventual generator
* return expression) is passed as the second parameter to onOpen().
*
* @param \Aerys\Request $request The HTTP request that instigated the handshake
* @param \Aerys\Response $response Used to set headers and/or reject the handshake
* @return mixed
*/
public function onHandshake(Request $request, Response $response) {
print "onHandshake\n";
}
/**
* Invoked when the full two-way websocket upgrade completes
*
* @param int $clientId A unique (to the current process) identifier for this client
* @param mixed $handshakeData The return value from onHandshake() for this client
* @return mixed
*/
public function onOpen(int $clientId, $handshakeData) {
print "onOpen\n";
}
/**
* Invoked when data messages arrive from the client
*
* @param int $clientId A unique (to the current process) identifier for this client
* @param \Aerys\WebsocketMessage $msg A stream of data received from the client
* @return mixed
*/
public function onData(int $clientId, Websocket\Message $msg) {
print $msg . "\n";
$this->clients[0]->send($msg);
}
/**
* Invoked when the close handshake completes
*
* @param int $clientId A unique (to the current process) identifier for this client
* @param int $code The websocket code describing the close
* @param string $reason The reason for the close (may be empty)
* @return mixed
*/
public function onClose(int $clientId, int $code, string $reason) {
print "client quit\n";
}
/**
* Invoked when the server is stopping
*
* If the application initialized resources in Websocket::onStart() this is the
* place to free them.
*
* There is no need to call Endpoint::close() in this method because the
* websocket endpoint automatically closes all connected clients before this application
* method is ever invoked.
*
* If this method is a Generator it will be resolved as a coroutine before the server
* is allowed to fully shutdown. Additionally, if this method returns a Promise the
* server will not shutdown until that promise resolves.
*
* @return mixed
*/
public function onStop() {
}
}
$router = router()
->route("GET", "/", function(Request $req, Response $res) {
$res->send("<html><body><h1>Hello, world.</h1></body></html>");
})
->route("GET", "/ws", websocket(new CMS));
(new Host)
->expose("*", 8005)
->use($router);