《Thinkphp5使用Socket服务》 入门篇

今天来说一下thinkphp5.0下使用wokerman的socket服务。
安装:
composer的安装方法,Windows下直接下个composer的应用程序,双击安装,环境变量同时给配置好了,Linux端命令安装,如我使用的Ubuntu,键入 sudo apt-get install composer -y 。
切换镜像:
composer切换到国内镜像,安装扩展可以提升一些速度。执行命令 composer config -g repo.packagist composer https://packagist.phpcomposer.com
更新:
进入到thinkphp项目的根目录下,执行 composer update 命令
下载:
接下来安装workerman ,执行 composer require topthink/think-worker 。

报错,根据报错信息,可以看出,framework版本不符合要求,导致安装失败。

如图可以看到,think-worker默认安装 2.0版本,而它需要5.1版本的framework,thinkphp5.0版本不被支持。

dev-master 版本,对framework没有要求,所以执行 composer require topthink/think-worker dev-master 安装即可。
如果要在win下做服务,继续执行 composer require workerman/workerman-for-win
绑定:
在根目录创建Worker.php作为启动服务文件,内容如下。

再创建push模块Worker控制器作为服务处理类

开启服务:
返回到项目根目录,执行命令 php Worker.php start -d 开启服务
注:这一步可能会报错

打开服务处理类文件,把ip地址换成0.0.0.0,然后重新执行php命令

测试:
打开Chrome 浏览器,在console里键入


大功告成。
本文只是初步连接websocket,至于投入项目使用,之后在写。
如果有帮到你,非常荣幸,请帮我点赞,谢谢。