websocket 每次刷新会先关闭后open 然后又关闭两个
来源:4-9 websocket服务优化
风行天下1
2018-11-16
<?php
/**
* Created by PhpStorm.
* User: brady
* Date: 2018/11/16
* Time: 15:37
*/
class Ws
{
const HOST = "0.0.0.0";
const PORT = "8812";
public $ws = null;
public function __construct($host,$port)
{
$this->ws = new swoole_websocket_server($host, $port);
$this->ws->on('open',[$this,"on_open"]);
$this->ws->on('message',[$this,"on_message"]);
$this->ws->on('close',[$this,"on_close"]);
$this->ws->set([
'document_root' => '/www/test/html',
'enable_static_handler' => true,
]);
$this->ws->start();
}
public function on_open($ws, $request)
{
var_dump( "on_open");
$ws->push($request->fd, "hello, test\n");
}
public function on_message($ws, $frame)
{
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
}
public function on_close($ws, $fd)
{
echo "client-{$fd} is closed\n";
}
}
$ws = new Ws('192.168.33.90',9502);
client-20 is closed
string(7) "on_open"
client-21 is closed
client-22 is closed
写回答
1回答
-
singwa
2018-11-16
刷新后肯定会关闭,然后重新链接。
00
相似问题