-
Notifications
You must be signed in to change notification settings - Fork 2
/
rfc6455.php
134 lines (124 loc) · 3.05 KB
/
rfc6455.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Execute the following command from a terminal to run the WebSocket server
* as a background service
*
* php -q /path/to/rfc6455.php [ >> /path/to/rfc6455.log ] & disown
**/
/**
* Simple console logger
* @return NULL
* @param $line string
**/
function trace($line) {
echo "\r".date('H:i:s').' ['.memory_get_usage(TRUE).'] '.$line.PHP_EOL;
}
if (PHP_SAPI!='cli') {
// Prohibit direct HTTP access
header('HTTP/1.1 404 Not Found');
die;
}
chdir(__DIR__);
ini_set('default_socket_timeout',3);
$fw=require('lib/base.php');
error_reporting(
(E_ALL|E_STRICT)&~(E_NOTICE|E_USER_NOTICE|E_WARNING|E_USER_WARNING)
);
$fw->DEBUG=2;
$fw->ONERROR=function($fw) {
trace($fw->get('ERROR.text'));
foreach (explode("\n",trim($fw->get('ERROR.trace'))) as $line)
trace($line);
};
// Instantiate the server
$ws=new CLI\WS('tcp://0.0.0.0:9000');
/* If you need a secure WebSocket server:
$ws=new CLI\WS(
'ssl://0.0.0.0:9000',
stream_context_create([
'ssl'=>[
'local_cert'=>'/path/to/.pem',
'verify_peer'=>FALSE,
'verify_peer_name'=>FALSE,
'allow_self_signed'=>TRUE
]
])
);
*/
$ws->
on('start',function($server) use($fw) {
trace('WebSocket server started');
$fw->write('tmp/ws.log','start'.PHP_EOL);
})->
on('error',function($server) use($fw) {
if ($err=socket_last_error()) {
trace(socket_strerror($err));
socket_clear_error();
}
if ($err=error_get_last())
trace($err['message']);
})->
on('stop',function($server) use($fw) {
trace('Shutting down');
$fw->write('tmp/ws.log','stop'.PHP_EOL,TRUE);
})->
on('connect',function($agent) use($fw) {
trace(
'(0x00'.$agent->uri().') '.$agent->id().' connected '.
'<'.(count($agent->server()->agents())+1).'>'
);
$fw->write('tmp/ws.log','connect'.PHP_EOL,TRUE);
})->
on('disconnect',function($agent) use($fw) {
trace('(0x08'.$agent->uri().') '.$agent->id().' disconnected');
if ($err=socket_last_error()) {
trace(socket_strerror($err));
socket_clear_error();
}
$fw->write('tmp/ws.log','disconnect'.PHP_EOL,TRUE);
})->
on('idle',function($agent) use($fw) {
$fw->write('tmp/ws.log','idle'.PHP_EOL,TRUE);
})->
on('receive',function($agent,$op,$data) use($fw) {
switch($op) {
case CLI\WS::Pong:
$text='pong';
break;
case CLI\WS::Text:
$data=trim($data);
case CLI\WS::Binary:
$text='data';
break;
}
trace(
'(0x'.str_pad(dechex($op),2,'0',STR_PAD_LEFT).
$agent->uri().') '.$agent->id().' '.$text.' received'
);
if ($op==CLI\WS::Text && $data) {
$in=json_decode($data,TRUE);
$agent->send(CLI\WS::Text,json_encode($in));
$fw->write('tmp/ws.log','receive'.PHP_EOL,TRUE);
}
})->
on('send',function($agent,$op,$data) use($fw) {
switch($op) {
case CLI\WS::Ping:
$text='ping';
break;
case CLI\WS::Text:
$data=trim($data);
case CLI\WS::Binary:
$text='data';
break;
}
trace(
'(0x'.str_pad(dechex($op),2,'0',STR_PAD_LEFT).
$agent->uri().') '.$agent->id().' '.$text.' sent'
);
if ($op==CLI\WS::Text) {
$out=json_decode($data,TRUE);
$fw->write('tmp/ws.log','send'.PHP_EOL,TRUE);
}
})->
run();