Swoole——创建TCP服务
启动TCP服务代码
<?php
/**
* 智慧公厕
*/
namespace Toilet\Action;
class IndexAction extends CommonAction
{
public function server() {
//创建Server对象,监听 127.0.0.1:25111端口
$serv = new \Swoole\Server("127.0.0.1", 25111);
//监听连接进入事件
$serv->on('Connect', function ($serv, $fd) {
echo "Client: Connect.\n";
setlog([$fd],'','连接','toilet.log');
});
//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, "Server: ".$data);
setlog([$data,$fd,$from_id],'','响应','toilet.log');
});
//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
echo "Client: Close.\n";
setlog([$fd],'','关闭','toilet.log');
});
//启动服务器
$serv->start();
}
}
执行
# php toilet.php Index/server
PHP Fatal error: Class 'Swoole\Server' not found in /home/wwwroot/default/smart/smart-community/Application/Toilet/Action/IndexAction.class.php on line 12
Fatal error: Class 'Swoole\Server' not found in /home/wwwroot/default/smart/smart-community/Application/Toilet/Action/IndexAction.class.php on line 12
Class 'Swoole\Server' not found
FILE: /home/wwwroot/default/smart/smart-community/Application/Toilet/Action/IndexAction.class.php(12)
报错了,因为swoole没有安装
安装swoole后
# php toilet.php Index/server
Client: Connect.
客户端测试
# telnet 127.0.0.1 25111
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
Server: hello
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2019-04-12 微信小程序,加载更多
2019-04-12 绑定手机号,绑定头像昵称
2019-04-12 先外后里,由上而下,由左而右,盒子布局
2018-04-12 ssh连接超时问题解决方案,每一种方案都可以
2018-04-12 使用git命令 (git reset --hard HEAD) 回退版本信息
2018-04-12 如何的退出无响应的 SSH 连接
2017-04-12 Vue v-if v-for v-bind v-on