set([
'worker_num' => 8, // worker进程数 cpu1-4倍
'max_request' => 10000, // 根据内存定义 https://wiki.swoole.com/#/server/setting
]);
/**
* $fd 客户端连接服务端的唯一标识 ID自增形式 有最大值
* $reactor_id 线程 id
*/
// 监听连接进入事件
$serv->on('Connect', function ($serv, $fd, $reactor_id) {
echo "Client: {$reactor_id} - {$fd} - Connect.\n";
});
/**
* $reactor_id 发送的目的地ID
*/
// 监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $reactor_id, $data) {
$serv->send($fd, "Server: {$reactor_id} - {$fd}".$data);
});
// 监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
echo "Client: Close.\n";
});
// 启动服务器
$serv->start();
//使用telnet工具测试server开启状态
connect("127.0.0.1", 9501)) exit("连接失败");
// php cli 常量
fwrite(STDOUT, "请输入消息:");
$msg = trim(fgets(STDIN));
// 发送消息给tcp server服务器
$client->send($msg);
// 接收来自server 的数据
$result = $client->recv();
echo $result;
查看端口占用程序命令 lsof -i :9501 / netstat -anvp tcp | grep 9501
https://wiki.swoole.com/#/start/start_udp_server
on('request', function($request, $response) {
$response->cookie('mingzi', 'neirong', time() + 1800);
$response->end(json_encode($request->get));
});
$http->start();
set([
'enable_static_handler' => true,
'document_root' => '/Applications/MAMP/htdocs/swoole学习/swoole_demo/data'
]);
$http->on('request', function($request, $response) {
$response->cookie('mingzi', 'neirong', time() + 1800);
$response->end(json_encode($request->get));
});
$http->start();
set([
'enable_static_handler' => true,
'document_root' => '/Applications/MAMP/htdocs/swoole学习/swoole_demo/data'
]);
// 监听ws连接打开事件
$server->on('open', 'onOpen');
function onOpen($server, $request) {
echo $request->fd;
};
// 监听ws消息事件
$server->on('message', function ($server, $frame) {
echo "recive from {$frame->fd} : {$frame->data}, opcode : {$frame->opcode}, fin : {$frame->finish}\n";
$server->push($frame->fd, '我收到了 回给你一句话');
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
websocket_html部分
第一个swoole-websocket-demo
测试 websocket
优化后的websocket
ws = new swoole_websocket_server("0.0.0.0", 8812);
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 监听ws连接事件
* @Author Jartin
* @Url string
* @DateTime 2020-03-22T19:23:40+0800
* @param [type] $ws [description]
* @param [type] $request [description]
* @return [type] [description]
*/
public function onOpen($ws, $request)
{
var_dump($request->fd);
}
/**
* 监听ws消息事件
* @Author Jartin
* @Url string
* @DateTime 2020-03-22T19:25:25+0800
* @param [type] $ws [description]
* @param [type] $frame [description]
* @return [type] [description]
*/
public function onMessage($ws, $frame)
{
echo "服务端监听到客户端的消息:{$frame->data}\n";
$ws->push($frame->fd, "服务端返回给客户端数据:" . date('Y-m-d H:i:s', time()));
}
/**
*
* @Author Jartin
* @Url string
* @DateTime 2020-03-22T19:28:44+0800
* @param [type] $ws [description]
* @param [type] $fd [description]
* @return [type] [description]
*/
public function onClose($ws, $fd)
{
echo "客户端ID:{$fd}\n";
}
}
$obj = new Ws();