websocket server based on swoole
PHP >= 7.0.0
swoole >= 1.7.9
composer
composer require jimmyfen/websocket-server
array(
'HOST' => '0.0.0.0', // 绑定监听域名
'PORT' => 9501, // 绑定监听端口
'SETTING' => array(
... // 此处为swoole设置参数,详情参考swoole文档
)
)
src
|-------conf // 配置文件,可在此查看默认配置
|-------lib
|-------logs // 默认swoole日志文件位置,若不存在且未改配置需手动创建
|-------process
// 文件index.php
<?php
require './vendor/autoload.php';
function onOpen($server, $request){
// do something
}
function onMessage($server, $frame){
// do something
}
function onClose($server, $fd){
// do something
}
function onRequest($request, $response){
// do something
}
// 监听事件,参考swoole文档(函数可以是对象对应的方法,参考call_user_func_array()函数)
$callable = [
'open' => 'onOpen',
'message' => 'onMessage',
'close' => 'onClose',
'request' => 'onRequest'
];
// 配置项,SETTING部分参考swoole文档
$config = [
'HOST' => '0.0.0.0',
'PORT' => 9501,
'SETTING' => [
'max_request' => 1000,
'work_num' => 2,
'pid_file' => './server.pid',
'log_file' => './console.log'
]
];
\Websocket\App::run($callable, $config);
?>
// 运行:php index.php start(不加参数可以看到具体的参数介绍)