嗯调了一下,怎么说呢,这个代码里面有很多小问题,比如:
1、类方法调用没有用 this
2、$this->user,$this->users 混乱
3、handshake 方法里面的变量 $buffer 没有声明赋值,实际上它应该是参数 $data
4、handshake 方法里面的 $skey 变量在后面的调用中写错了
...
后面的我都没记录了,都是小问题,改了就能运行了:
附上代码
服务端:
error_reporting(E_ERROR);
class ws
{
public $sock;
public $socks;
public $users;
function send($socket, $msg)
{
if ($socket === "all") {
foreach ($this->users as $key => $value) {
socket_write($this->users[ $key ]['client'], $msg, strlen($msg));
}
}
socket_write($socket, $msg, strlen($msg));
}
function handshake($socket, $data)
{
$buffer = $data;
$secretkey