-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketServer.php
208 lines (169 loc) · 6.68 KB
/
WebSocketServer.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
<?php
/**
* WebSocketServer Class
* based on http://code.google.com/p/phpwebsocket/
* @author DerFichtl AT gmail.com / @DerFichtl on Twitter
*/
Class WebSocketServer {
protected $address = null;
protected $port = null;
protected $users = array();
protected $master = null;
protected $sockets = array();
protected $callback = null;
protected $maxConnection = 99;
public function __construct($address, $port, $callback) {
$this->address = $address;
$this->port = $port;
$this->callback = $callback;
$this->connectMaster($address, $port);
}
public function getUsers() {
return $this->users;
}
protected function connectMaster() {
$this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
$this->sockets[] = $this->master;
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
socket_bind($this->master, $this->address, $this->port) or die("socket_bind() failed");
socket_listen($this->master, 20) or die("socket_listen() failed");
return $this->master;
}
public function run() {
while(true){
$changed = $this->sockets;
socket_select($changed, $write=NULL, $except=NULL,NULL);
foreach($changed as $socket){
if ($socket == $this->master) {
$client = socket_accept($this->master);
if($client < 0){ console("socket_accept() failed"); continue; }
else{ $this->connect($client); }
} else {
$bytes = @socket_recv($socket, $buffer, 2048, 0);
if($bytes == 0) {
$this->disconnect($socket);
} else {
$user = $this->getUserBySocket($socket);
if(! $user->handshake) {
$user->doHandshake($buffer);
} else {
$user->lastAction = time();
// call the callback function
if ($this->callback) {
call_user_func($this->callback, $user, $this->unwrap($buffer), $this);
}
}
}
}
}
}
}
public function connect($socket) {
$this->users[] = new WebSocketUser($socket);
$this->sockets[] = $socket;
}
public function disconnect($socket) {
if ($this->users) {
$found=null;
$n = count($this->users);
for($i=0;$i<$n;$i++){
if($this->users[$i]->socket == $socket){ $found=$i; break; }
}
if(!is_null($found)){ array_splice($this->users, $found, 1); }
$index = array_search($socket, $this->sockets);
socket_close($socket);
$this->say($socket." DISCONNECTED!");
if($index>=0){ array_splice($this->sockets, $index, 1); }
}
}
public function getUserBySocket($socket) {
$found=null;
foreach($this->users as $user) {
if($user->socket==$socket) {
$found=$user;
break;
}
}
return $found;
}
public function send($client, $msg){
$msg = $this->wrap($msg);
$this->say("> ". $msg);
@socket_write($client, $msg, strlen($msg));
}
private function say($msg="") { var_dump($msg); echo "\n"; }
private function wrap($msg="") { return chr(0).$msg.chr(255); }
private function unwrap($msg="") { return substr($msg, 1, strlen($msg)-2); }
}
/**
* WebSocketUser Class
*/
class WebSocketUser {
public $id = null;
public $socket = null;
public $handshake = false;
public $ip = null;
public $lastAction = null;
public $data = array();
public function __construct($socket) {
$this->id = uniqid();
$this->socket = $socket;
socket_getpeername($socket, $ip);
$this->ip = $ip;
}
public function doHandshake($buffer) {
list($resource, $headers, $securityCode) = $this->handleRequestHeader($buffer);
$securityResponse = '';
if (isset($headers['Sec-WebSocket-Key1']) && isset($headers['Sec-WebSocket-Key2'])) {
$securityResponse = $this->getHandshakeSecurityKey($headers['Sec-WebSocket-Key1'], $headers['Sec-WebSocket-Key2'], $securityCode);
}
if ($securityResponse) {
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: WebSocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Origin: " . $headers['Origin'] . "\r\n" .
"Sec-WebSocket-Location: ws://" . $headers['Host'] . $resource . "\r\n" .
"\r\n".$securityResponse;
} else {
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: WebSocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: " . $headers['Origin'] . "\r\n" .
"WebSocket-Location: ws://" . $headers['Host'] . $resource . "\r\n" .
"\r\n";
}
$msg = $upgrade.chr(0);
@socket_write($this->socket, $msg, strlen($msg));
$this->handshake = true;
return true;
}
private function handleSecurityKey($key) {
preg_match_all('/[0-9]/', $key, $number);
preg_match_all('/ /', $key, $space);
if ($number && $space) {
return implode('', $number[0]) / count($space[0]);
}
return '';
}
private function getHandshakeSecurityKey($key1, $key2, $code) {
return md5(
pack('N', $this->handleSecurityKey($key1)).
pack('N', $this->handleSecurityKey($key2)).
$code,
true
);
}
private function handleRequestHeader($request) {
$resource = $code = null;
preg_match('/GET (.*?) HTTP/', $request, $match) && $resource = $match[1];
preg_match("/\r\n(.*?)\$/", $request, $match) && $code = $match[1];
$headers = array();
foreach(explode("\r\n", $request) as $line) {
if (strpos($line, ': ') !== false) {
list($key, $value) = explode(': ', $line);
$headers[trim($key)] = trim($value);
}
}
return array($resource, $headers, $code);
}
}