forked from oshynsong/web
/
websocket.class.php
315 lines (281 loc) · 7.07 KB
/
websocket.class.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
*@project Websocket (server endpoint by php implements)
*@author Oshyn Song <dualyangsong@gmail.com>
*@version 1.0beta
*@time 2014-3
*@usage (new WebSocket($address, $port))->run()
*/
class WebSocket
{
var $master;
var $sockets = array();
var $users = array();
var $debug = true;
protected $address = 'localhost';
protected $port = 8080;
/**
*@name constructor
*@access public
*/
public function __construct($a, $p)
{
if ($a == 'localhost')
$this->address = $a;
else if (preg_match('/^[\d\.]*$/is', $a))
$this->address = long2ip(ip2long($a));
else
$this->address = $p;
if (is_numeric($p) && intval($p) > 1024 && intval($p) < 65536)
$this->port = $p;
else
die ("Not valid port:" . $p);
$this->createSocket();
array_push($this->sockets, $this->master);
}
/**
*@name run
*@desc wait for the client to connect and process
*/
public function run()
{
while(true)
{
$socketArr = $this->sockets;
$write = NULL;
$except = NULL;
socket_select($socketArr, $write, $except, NULL); //select the socket with message automaticly
//if handshake choose the master
foreach ($socketArr as $socket)
{
if ($socket == $this->master)
{
$client = socket_accept($this->master);
if ($client < 0)
{
$this->log("socket_accept() failed");
continue;
}
else
{
$this->connect($client);
}
}
else
{
$this->log("----------New Frame Start-------");
$bytes = @socket_recv($socket,$buffer,2048,0);
if ($bytes == 0)
{
$this->disconnect($socket);
}
else
{
$user = $this->getUserBySocket($socket);
if (!$user->handshake)
{
$this->doHandShake($user, $buffer);
}
else
{
$this->process($user, $buffer);
}
}
}
}
}
}
/**
*@name process
*@desc Simple process method just return the info from the client,
* this can be override in real application by extends this class
*/
protected function process($user, $msg)
{
$msg = $this->unwrap($user->socket, $msg);
$this->say('< ' . $msg);
$this->send($user->socket, $msg);
}
public function send($client, $msg)
{
$this->say("> Resource id:" . substr('' . $client, 13) . ' ' . $msg);
$msg = $this->wrap($msg);
@socket_write($client, $msg, strlen($msg));
$this->log("! " . strlen($msg));
}
/**
*@name connect
*@access public
*@desc connect to the client and push the client socket into array
*/
public function connect($clientSocket)
{
$user = new User();
$user->id = uniqid();
$user->socket = $clientSocket;
array_push($this->users,$user);
array_push($this->sockets,$clientSocket);
$this->log($user->socket . " CONNECTED!" . date("Y-m-d H-i-s"));
}
/**
*@name disconnect
*@access public
*@desc disconnect to the specific client socket
*/
public function disconnect($clientSocket)
{
$found = null;
$n = count($this->users);
for($i = 0; $i<$n; $i++)
{
if($this->users[$i]->socket == $clientSocket)
{
$found = $i;
break;
}
}
$index = array_search($clientSocket,$this->sockets);
if(!is_null($found))
{
array_splice($this->users, $found, 1);
array_splice($this->sockets, $index, 1);
socket_close($clientSocket);
$this->say($clientSocket." DISCONNECTED!");
}
}
/**
*@name getHeaders
*@access public
*@param string req
*@return array ($resource,$host,$origin,$key)
*/
public function getHeaders($req)
{
$r = $h = $o = null;
if(preg_match("/GET (.*) HTTP/" , $req, $match))
$r = $match[1];
if(preg_match("/Host: (.*)\r\n/" , $req, $match))
$h = $match[1];
if(preg_match("/Origin: (.*)\r\n/", $req, $match))
$o = $match[1];
if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $req, $match))
$key = $match[1];
return array($r, $h, $o, $key);
}
protected function unwrap($clientSocket, $msg="")
{
$opcode = ord(substr($msg, 0, 1)) & 0x0F;
$payloadlen = ord(substr($msg, 1, 1)) & 0x7F;
$ismask = (ord(substr($msg, 1, 1)) & 0x80) >> 7;
$maskkey = null;
$oridata = null;
$decodedata = null;
//close socket
if ($ismask != 1 || $opcode == 0x8)
{
$this->disconnect($clientSocket);
return null;
}
//get the masking key and masked data
if ($payloadlen <= 125 && $payloadlen >= 0)
{
$maskkey = substr($msg, 2, 4);
$oridata = substr($msg, 6);
}
else if ($payloadlen == 126)
{
$maskkey = substr($msg, 4, 4);
$oridata = substr($msg, 8);
}
else if ($payloadlen == 127)
{
$maskkey = substr($msg, 10, 4);
$oridata = substr($msg, 14);
}
$len = strlen($oridata);
for($i = 0; $i < $len; $i++) //decode the masked data
{
$decodedata .= $oridata[$i] ^ $maskkey[$i % 4];
}
return $decodedata;
}
protected function wrap($msg="", $opcode = 0x1)
{
//control bit, default is 0x1(text data)
$firstByte = 0x80 | $opcode;
$encodedata = null;
$len = strlen($msg);
if (0 <= $len && $len <= 125)
$encodedata = chr(0x81) . chr($len) . $msg;
else if (126 <= $len && $len <= 0xFFFF)
{
$low = $len & 0x00FF;
$high = ($len & 0xFF00) >> 8;
$encodedata = chr($firstByte) . chr(0x7E) . chr($high) . chr($low) . $msg;
}
return $encodedata;
}
private function createSocket()
{
$this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or die("socket_create() failed:".socket_strerror(socket_last_error()));
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1)
or die("socket_option() failed".socket_strerror(socket_last_error()));
socket_bind($this->master, $this->address, $this->port)
or die("socket_bind() failed".socket_strerror(socket_last_error()));
socket_listen($this->master,20)
or die("socket_listen() failed".socket_strerror(socket_last_error()));
$this->say("Server Started : ".date('Y-m-d H:i:s'));
$this->say("Master socket : ".$this->master);
$this->say("Listening on : ".$this->address." port ".$this->port."\n");
}
private function doHandShake($user, $buffer)
{
$this->log("\nRequesting handshake...");
$this->log($buffer);
list($resource, $host, $origin, $key) = $this->getHeaders($buffer);
//websocket version 13
$acceptKey = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
$this->log("Handshaking...");
$upgrade = "HTTP/1.1 101 Switching Protocol\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: " . $acceptKey . "\r\n\r\n"; //必须以两个回车结尾
$this->log($upgrade);
$sent = socket_write($user->socket, $upgrade, strlen($upgrade));
$user->handshake=true;
$this->log("Done handshaking...");
return true;
}
private function getUserBySocket($socket)
{
$found=null;
foreach($this->users as $user)
{
if ($user->socket == $socket)
{
$found = $user;
break;
}
}
return $found;
}
public function say($msg = "")
{
echo $msg . "\n";
}
public function log($msg = "")
{
if ($this->debug)
{
echo $msg . "\n";
}
}
}
class User
{
var $id;
var $socket;
var $handshake;
}
?>