首页
友人帐
留言板
关于
Search
1
IDE Eval Resetter:JetBrains 全家桶无限试用插件
381 阅读
2
影视资源采集站收录大全
302 阅读
3
linux安装或升级protoc
224 阅读
4
VFM 3.7.5 源码 - 一个极简的 PHP 私人云盘!
161 阅读
5
批量采集美女写真等图片做图片站
149 阅读
谈天说地
程序源码
技术教程
成品源码
登录
Search
标签搜索
PHP
linux
源码
go
windows
centos
原创
mysql
微信
激活
采集
宝塔
绿色版
API
解析
SDK
图片
破解
域名
html
云青
累计撰写
150
篇文章
累计收到
78
条评论
首页
栏目
谈天说地
程序源码
技术教程
成品源码
页面
友人帐
留言板
关于
搜索到
1
篇与
的结果
2022-12-05
TP5使用workerman开启websocket服务
前言之前代码使用websocket方式是直接下载workman文件放入vendor中进行使用,这种方式处理及其容易出现问题,比如linux运行正常而windows无法使用,每次部署代码拉取依赖包都会覆盖等奇奇怪怪的问题安装依赖 通过Composer.exe安装相关依赖:composer require topthink/think-worker 如果报错,如下:composer require topthink/think-worker Using version ^2.0 for topthink/think-worker ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 topthink/think-worker v2.0.5 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - topthink/think-worker v2.0.4 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - topthink/think-worker v2.0.3 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - topthink/think-worker v2.0.2 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - topthink/think-worker v2.0.1 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - topthink/think-worker v2.0.0 requires topthink/framework 5.1.* -> satisfiable by topthink/framework[5.1.x-dev, v5.1-beta.1, v5.1-rc.1, v5.1-rc.2, v5.1-rc.3, v5.1.0, v5.1.1, v5.1.10, v5.1.11, v5.1.12, v5.1.13, v5.1.14, v5.1.15, v5.1.16, v5.1.17, v5.1.18, v5.1.19, v5.1.2, v5.1.3, v5.1.4, v5.1.5, v5.1.6, v5.1.7, v5.1.8, v5.1.9] but these conflict with your requirements or minimum-stability. - Installation requestfortopthink/think-worker ^2.0 -> satisfiable by topthink/think-worker[v2.0.0, v2.0.1, v2.0.2, v2.0.3, v2.0.4, v2.0.5].Installation failed, reverting ./composer.json to its original content. Installation failed, reverting ./composer.json to its original content. 则是版本不匹配,用低版本的workerman,更改如下:composer require topthink/think-worker=1.0.* 如果是windows系统,还需安装依赖:composer require workerman/workerman-for-win 启动服务文件在项目根目录里面新增server.php文件<?php define('APP_PATH', __DIR__ . '/../application/'); define("BIND_MODULE", "server/Screen"); // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';增加控制器<?php namespace app\server\controller; use think\worker\Server; class Screen extends Server { protected $socket = 'websocket://127.0.0.1:2346'; private $all = []; /** * 收到信息 * @param $connection * @param $data */ public function onMessage($connection, $data) { // 广播 foreach ($this->all as $v) { $v->send(json_encode("广播")); } // $connection->send("收到消息啦,您的id:" . $connection->id); } /** * 当连接建立时触发的回调函数 * @param $connection */ public function onConnect($connection) { // 保存所有已连接的客户端id $this->all[$connection->id] = $connection; } /** * 当连接断开时触发的回调函数 * @param $connection */ public function onClose($connection) { // 去除断开连接的客户端 unset($this->all[$connection->id]); } /** * 当客户端的连接上发生错误时触发 * @param $connection * @param $code * @param $msg */ public function onError($connection, $code, $msg) { echo "error $code $msg"; } /** * 每个进程启动 * @param $worker */ public function onWorkerStart($worker) { } }启动监听服务打开cmd或bash执行以下代码php server.php测试打开chrome浏览器, 先打开127.0.0.1域名下的网页( js跨域不能通讯),按F12打开调试控制台, 在console一栏输入(或者把下面代码放入到html页面用js运行)ws = new WebSocket("ws://127.0.0.1:2346"); ws.onopen = function() { alert("连接成功"); ws.send('tom'); alert("给服务端发送一个字符串: tom"); }; ws.onmessage = function(e) { alert("收到服务端的消息: " + e.data); }; ws.send('保持连接, 发第二次信息, 查看服务器回应');
2022年12月05日
27 阅读
0 评论
0 点赞