//参考地址http://www.jianshu.com/p/fedbb9d2d999php代码:$serv
=
new
swoole_websocket_server(
"127.0.0.1"
,3999);
//服务的基本设置
$serv
->set(
array
(
'worker_num'
=> 2,
'reactor_num'
=>8,
'task_worker_num'
=>1,
'dispatch_mode'
'debug_mode'
=> 1,
'daemonize'
=> true,
'log_file'
=> __DIR__.
'/log/webs_swoole.log'
,
'heartbeat_check_interval'
=> 60,
'heartbeat_idle_time'
=> 600,
));
->on(
'connect'
function
$fd
){
// echo "client:$fd Connect.".PHP_EOL;
});
//测试receive
"receive"
(swoole_server
$from_id
$data
// echo "receive#{$from_id}: receive $data ".PHP_EOL;
'open'
$server
$req
) {
// echo "server#{$server->worker_pid}: handshake success with fd#{$req->fd}".PHP_EOL;;
// echo PHP_EOL;
'message'
$frame
// echo "message: ".$frame->data.PHP_EOL;
$msg
=json_decode(
->data,true);
switch
[
'type'
]){
case
'login'
:
->push(
->fd,
"欢迎欢迎~"
);
break
;
default
}
'fd'
]=
->fd;
->task(
"workerstart"
$workerid
// echo "workerstart: ".$workerid.PHP_EOL;
"task"
"on_task"
"finish"
$task_id
return
'close'
// echo "connection close: ".$fd.PHP_EOL;
->start();
on_task(
$send_msg
"说:我来了~"
"说:{$data['msg']['speak']}"
foreach
->connections
as
$conn
if
!=
strpos
'msg'
][
'name'
],
"游客"
)===0){
$name
].
"_"
.
];
else
{
"我"
on_finish(
true;
}前端代码:
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
"Content-Type"cOntent="text/html; charset=UTF-8">
"Content-Type"
cOntent=
"text/html; charset=UTF-8"
WebSocket Test
昵称:"text"id="name" size="5" value="游客"/> "text"id="content">
"text"
id=
"name"
size=
"5"
value=
/> "text"id="content">
"content"
"speak_to_all()">发送
"speak_to_all()"
>发送
"message"php string">"overflow-x:hidden"rows="10" cols="50">
"message"
php string">"overflow-x:hidden"
rows=
"10"
cols=
"50"
"output">
"output"