1 Star 0 Fork 0

北晨 / swoole 弹幕

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.php 2.95 KB
一键复制 编辑 原始数据 按行查看 历史
北晨 提交于 2019-04-18 14:04 . 视频弹幕
<?php
class server
{
private $serv;
public function __construct ()
{
$this -> serv = new swoole_websocket_server("0.0.0.0", 9501);
$this -> serv -> set([
'worker_num' => 2,
'max_request' => 4,
'task_worker_num' => 4,
'dispatch_mode' => 4,
'daemonize' => false,
]);
$this -> serv -> on('Start', [ $this, 'onStart' ]);
$this -> serv -> on('Open', [ $this, 'onOpen' ]);
$this -> serv -> on('Message', [ $this, 'onMessage' ]);
$this -> serv -> on('Close', [ $this, 'onClose' ]);
$this -> serv -> on('Task', [ $this, 'onTask' ]);
$this -> serv -> on('Finish', [ $this, 'onFinish' ]);
$this -> serv -> start();
}
public function onStart ($serv)
{
echo "### onStart ###" . PHP_EOL;
echo "SWOOLE" . SWOOLE_VERSION . "服务已启动" . PHP_EOL;
echo "master_pid: {$serv->master_pid}" . PHP_EOL;
echo "manager_pid: {$serv->manager_pid}" . PHP_EOL;
echo "###" . PHP_EOL . PHP_EOL;
}
public function onOpen ($serv, $request)
{
echo "### onOpen ###" . PHP_EOL;
echo "server: handshake success with fd{$request->fd}" . PHP_EOL;
$serv -> task([
'type' => 'login'
]);
echo "###" . PHP_EOL . PHP_EOL;
}
public function onTask ($serv, $task_id, $form_id, $data)
{
echo "### onTask ###" . PHP_EOL;
echo "#{$serv->worker_id} onTask:[PID={$serv->worker_pid}]: task_id={$task_id}" . PHP_EOL;
$msg = "";
switch ($data['type']) {
case 'login':
$msg = "我来了......";
break;
case 'speak':
$msg = $data['msg'];
break;
}
foreach ($serv -> connections as $fd) {
$connectionInfo = $serv -> connection_info($fd);
if ($connectionInfo['websocket_status'] == 3) {
//长度最大不得超过2M
$serv -> push($fd, $msg);
}
}
$serv -> finish($data);
echo "###" . PHP_EOL . PHP_EOL;
}
public function onMessage ($serv, $frame)
{
echo "### onMessage ###" . PHP_EOL;
echo "receive from fd{$frame->fd}:{$frame->data},opcode:{$frame->opcode},finish:{$frame->finish}" . PHP_EOL;
$serv -> task([ 'type' => 'speak', 'msg' => $frame -> data ]);
echo "###" . PHP_EOL . PHP_EOL;
}
public function onFinish ($serv, $task_id, $data)
{
echo "### onFinish ###" . PHP_EOL;
echo "Task {$task_id} 已完成" . PHP_EOL;
echo "###" . PHP_EOL . PHP_EOL;
}
public function onClose ($serv, $fd)
{
echo "### onClose ###" . PHP_EOL;
echo "client {$fd} closed" . PHP_EOL;
echo "###" . PHP_EOL . PHP_EOL;
}
}
$server = new server();
PHP
1
https://gitee.com/gogochen/swoolebarrage.git
git@gitee.com:gogochen/swoolebarrage.git
gogochen
swoolebarrage
swoole 弹幕
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891