class
WS{
CONST HOST =
"192.168.33.10"
;
CONST PORT =9504;
public
$ws
= null;
public
function
__construct()
{
$this
->ws =
new
swoole_websocket_server(self::HOST,self::PORT);
$this
->ws->set([
'worker_num'
=> 8,
'task_worker_num'
=> 8,
'enable_static_handler'
=> true,
'document_root'
=>
"/vagrant_data/web/singwa"
,
]);
$this
->ws->on(
'open'
,[
$this
,
'onOpen'
]);
$this
->ws->on(
'task'
,[
$this
,
'onMessage'
]);
$this
->ws->on(
'finish'
,[
$this
,
'onFinish'
]);
$this
->ws->on(
'message'
,[
$this
,
'onMessage'
]);
$this
->ws->on(
'close'
,[
$this
,
'onClose'
]);
$this
->ws->start();
}
public
function
onOpen(
$ws
,
$request
){
echo
"open connetc {$request->fd}\n"
;
}
public
function
onMessage(
$ws
,
$frame
){
echo
"返回数据信息:"
.
$frame
->data.
"opcode {$frame->opcode}:fin:{$frame->finish}\n"
;
$data
= [
'task'
=> 1,
'fd'
=>
$frame
->fd,
];
$ws
->task(
$data
);
$ws
->push(
$frame
->fd,
"return connect success返回客户端数据信息6{$frame->data}"
);
}
public
function
onTask(
$serv
,
$task_id
,
$worker_id
,
$data
){
print
(
$data
);
sleep(10);
return
"on finish work"
;
}
public
function
onFinish(
$serv
,
$task_id
,
$data
){
echo
"onFinish:{$task_id}\n"
;
print
(
"得到的数据是:"
.
$data
);
}
public
function
onClose(
$ws
,
$fd
){
echo
"close:{$fd}client\n"
;
}
}
$WS
=
new
WS();