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(server started : .date('y-m-d h:i:s'));
$this->say(listening on : .$address. port .$port);
$this->say(master socket : .$this->master.\n);
while(true){
$changed = $this->sockets;
$write=null;
$except=null;
socket_select($changed,$write,$except,null);
foreach($changed as $socket){
if($socket==$this->master){
$client=socket_accept($this->master);
if($clientsay(socket_accept() failed); continue; }
else{ $this->connect($client); }
}
else{
$bytes = @socket_recv($socket,$buffer,2048,0);
if($bytes==0){ $this->disconnect($socket); }
else{
$this->say('their words: '.$buffer);
$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));
$this->say(! .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->say($socket. connected!);
$this->say(date(d/n/y ).at .date(h:i:s t));
}
function disconnect($socket){
$found=null;
$n=count($this->users);
for($i=0;$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); }
}
/*old way:
function dohandshake($user,$buffer){
$this->say(\nrequesting handshake...);
$this->say($buffer);
list($resource,$host,$origin,$key1,$key2,$l8b) = $this->getheaders($buffer);
$this->say(handshaking...);
//$port = explode(:,$host);
//$port = $port[1];
//$this->say($origin.\r\n.$host);
$upgrade = http/1.1 101 websocket protocol handshake\r\n .
upgrade: websocket\r\n .
connection: upgrade\r\n .
sec-websocket-origin: . $origin . \r\n .
sec-websocket-location: ws:// . $host . $resource . \r\n .
\r\n .
$this->calckey($key1,$key2,$l8b) . \r\n;// .
//\r\n;
socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
$user->handshake=true;
$this->say($upgrade);
$this->say(done handshaking...);
return true;
}*/
function dohandshake($user,$buffer){
$this->say(\nstart shakehanding\nfollowing is from client\n);
$this->say($buffer);
$this->say(\nfollowing is ours\n);
if(preg_match(/sec-websocket-key1/,$buffer)){
list($resource,$host,$origin,$key1,$key2,$l8b) = $this->getheaders($buffer);
$upgrade = http/1.1 101 websocket protocol handshake\r\n .
upgrade: websocket\r\n .
connection: upgrade\r\n .
sec-websocket-origin: . $origin . \r\n .
sec-websocket-location: ws:// . $host . $resource . \r\n .
\r\n .$this->calckey($key1,$key2,$l8b) . \r\n;
}
elseif(preg_match(/sec-websocket-key:/,$buffer)){
preg_match(/sec-websocket-key: (.*)\r\n/,$buffer,$match);
$key=$match[1];
$acceptkey=base64_encode(sha1($key.'258eafa5-e914-47da-95ca-c5ab0dc85b11',true));
$upgrade=http/1.1 101 switching protocols\r\n.
upgrade: websocket\r\n.
connection: upgrade\r\n.
sec-websocket-accept: .$acceptkey.\r\n\r\n;
}
$this->say($upgrade);
socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
$user->handshake=true;
$this->say(finish shakehanding.);
return true;
}
function calckey($key1,$key2,$l8b){
//get the numbers
preg_match_all('/([/d]+)/', $key1, $key1_num);
preg_match_all('/([/d]+)/', $key2, $key2_num);
//number crunching [/bad pun]
$this->say(key1: . $key1_num = implode($key1_num[0]) );
$this->say(key2: . $key2_num = implode($key2_num[0]) );
//count spaces
preg_match_all('/([ ]+)/', $key1, $key1_spc);
preg_match_all('/([ ]+)/', $key2, $key2_spc);
//how many spaces did it find?
$this->say(key1 spaces: . $key1_spc = strlen(implode($key1_spc[0])) );
$this->say(key2 spaces: . $key2_spc = strlen(implode($key2_spc[0])) );
if($key1_spc==0|$key2_spc==0){ $this->say(invalid key);return; }
//some math
$key1_sec = pack(n,$key1_num / $key1_spc); //get the 32bit secret key, minus the other thing
$key2_sec = pack(n,$key2_num / $key2_spc);
//this needs checking, i'm not completely sure it should be a binary string
return md5($key1_sec.$key2_sec.$l8b,1); //the result, i think
}
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-key1: (.*)\r\n/,$req,$match)){ $this->say(sec key1: .$sk1=$match[1]); }
if(preg_match(/sec-websocket-key2: (.*)\r\n/,$req,$match)){ $this->say(sec key2: .$sk2=$match[1]); }
if($match=substr($req,-8)) { $this->say(last 8 bytes: .$l8b=$match); }
return array($r,$h,$o,$sk1,$sk2,$l8b);
}
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 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;
}
(请格式化上面的代码)
以上的代码源自http://blog.csdn.net/trace332/article/details/6325986
不过很明显,csdn的那位楼主有许多语法错误,还有websocket的协议版本低。
我修改后新旧协议都能正常实现握手,不过关于收发数据还是无从下手,恳请大虾们赐教一二.
(新协议收发的是二进制数据,详细的另见其他文献)
(我只想收发utf-8字符)
(请也做到新旧都能兼容)
(谢谢!)