为什么我就打开一个页面,刷新的时候,提示关闭多个client?
来源:4-9 websocket服务优化
弱弱小乖乖
2018-04-04
namespace ws;
class ws
{
const host = "0.0.0.0";
const PORT = 8812;
public $ws = null;
public function __construct()
{
$this->ws = new \swoole_websocket_server(self::host, self::PORT);
$this->set();
$this->ws->on('open', [$this, 'onOpen']);
$this->ws->on('message', [$this, 'onMessage']);
$this->ws->on('close', [$this, 'onClose']);
$this->ws->start();
}
public function set(){
$this->ws->set([
'document_root' => '/var/www/swoole/data',
'enable_static_handler' => true,
]);
}
/**
* 当WebSocket客户端与服务器建立连接并完成握手后会回调此函数。
*/
public function onOpen($ws, $request){
echo "open: fd{$request->fd}\n";
}
/**
* 当服务器收到来自客户端的数据帧时会回调此函数。
* @param $ws
* @param $frame swoole_websocket_frame对象,包含了客户端发来的数据帧信息
* User: hhcycj
*/
public function onMessage($ws, $frame){
echo "message: receive from {$frame->fd}:{$frame->data}, opcode:{$frame->opcode}, finish:{$frame->finish}\n";
$this->ws->push($frame->fd, "this is server");
}
/**
* @param $ser
* @param $fd
* User: hhcycj
*/
public function onClose ($ser, $fd) {
echo "client {$fd} closed\n";
}
}
$obj = new ws();
写回答
3回答
-
风行天下1
2018-11-16
难道是三次握手
client-20 is closed
string(7) "on_open"
client-21 is closed
client-22 is closed
我的每次刷新也是
00 -
qq_四年_8
2018-06-11
这个问题我也想问一下,应该是worker_num 的数目的影响,但是解决方案还是没弄出来
00 -
弱弱小乖乖
提问者
2018-04-04

为什么呢?
022018-04-16
相似问题
