forked from GeorgeNava/phpwebsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.class.php
143 lines (126 loc) · 4.4 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
<?php
// Usage:
// $ws = new WebSocket("localhost",12345);
// $ws->listen();
class WebSocket{
var $master;
var $sockets = array();
var $users = array();
var $debug = false;
function __construct($address,$port){
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
socket_bind($this->master, $address, $port) or die("socket_bind() failed");
socket_listen($this->master,20) or die("socket_listen() failed");
$this->sockets[] = $this->master;
$this->say("---------------\n");
$this->say("Server Started : ".date('Y-m-d H:i:s'));
$this->say("Listening on : ".$address." port ".$port);
$this->say("Master socket : ".$this->master);
$this->say("---------------\n");
}
function listen(){
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){ $this->log("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){ $this->dohandshake($user,$buffer); }
else{ $this->process($user,$this->unwrap($buffer)); }
}
}
}
}
}
function process($user,$msg){
/* Extend and modify this method to suit your needs */
/* Basic usage is to echo incoming messages back to client */
$this->send($user->socket,$msg);
}
function send($client,$msg){
$this->say("> ".$msg);
$msg = $this->wrap($msg);
socket_write($client,$msg,strlen($msg));
}
function broadcast($msg,$except=null){
$this->say(">>".$msg);
foreach($this->users as $user){
if($user->socket==$except){ continue; } /* exclude from broadcast */
$msg = $this->wrap($msg);
socket_write($user->socket,$msg,strlen($msg));
}
}
function connect($socket){
$user = new User();
$user->id = uniqid();
$user->socket = $socket;
array_push($this->users,$user);
array_push($this->sockets,$socket);
$this->log($socket." CONNECTED!");
}
function disconnect($socket){
$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->log($socket." DISCONNECTED!");
if($index>=0){ array_splice($this->sockets,$index,1); }
}
function dohandshake($user,$buffer){
$this->log("\nRequesting handshake...");
$this->log($buffer);
list($resource,$host,$origin) = $this->getheaders($buffer);
$this->log("Handshaking...");
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: WebSocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: " . $origin . "\r\n" .
"WebSocket-Location: ws://" . $host . $resource . "\r\n" .
"\r\n";
socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
$user->handshake=true;
$this->log($upgrade);
$this->log("Done handshaking...");
return true;
}
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]; }
return array($r,$h,$o);
}
function getuserbysocket($socket){
$found=null;
foreach($this->users as $user){
if($user->socket==$socket){ $found=$user; break; }
}
return $found;
}
function say($msg=""){ echo $msg."\n"; }
function log($msg=""){ if($this->debug){ echo $msg."\n"; } }
function wrap($msg=""){ return chr(0).$msg.chr(255); }
function unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }
}
class User{
var $id;
var $socket;
var $handshake;
}
?>