1.下载GatewayWorker www.workerman.net/doc/gateway…
安装如图下载解压就行
以次开启端口: 8282,1238,2900,2901,2902,2903
启动
以debug(调试)方式启动
php start.php start
以daemon(守护进程)方式启动
php start.php start -d
停止
php start.php stop
重启
php start.php restart
平滑重启
php start.php reload
查看状态
php start.php status
debug和daemon方式区别
1、以debug方式启动,代码中echo、var_dump、print等打印函数会直接输出在终端。
2、以daemon方式启动,代码中echo、var_dump、print等打印会默认重定向到/dev/null文件,可以通过设置Worker::$stdoutFile = ‘/your/path/file’;来设置这个文件路径。
3、以debug方式启动,终端关闭后workerman会随之关闭并退出。
4、以daemon方式启动,终端关闭后workerman继续后台正常运行。
业务开发只需要关注 Applications/项目/Events.php一个文件即可。
2.默认ThinkPHP6已经安装好了 安装扩展
composer require workerman/gatewayclient复制代码
创建一个控制器
namespace app\api\controller;use GatewayClient\Gateway;use think\Request;use think\facade\db;USE think\facade\Cookie;class Swoole extends Base{public function initialize(){header('Access-Control-Allow-Origin: *');header("Access-Control-Allow-Headers: token,random,Origin, X-Requested-With, Content-Type, Accept");header('Access-Control-Allow-Methods: POST,GET');if(request()->isOptions()){exit();}Gateway::$registerAddress = "192.168.3.116:1238";}public function index(Request $request){$post = $request->post();$user = Db::name('user')->field('id,name')->where('status',0)->find();if (!$user) {$data = ['style'=>'yue',//you'data' => '人数已满,你连个屁','name' => '机器人'];return json($data);}Db::name('user')->where('id',$user['id'])->update(['client_id'=>$post['Client_id'],'status'=>1]);$data = ['style'=>'me',//you'data' => '链接成功','name' => $user['name']];return json($data);}public function send(Request $request){$post = $request->post();$name = Db::name('user')->field('name')->where('client_id',$post['client_id'])->find();$cid = Db::name('user')->where('status',1)->where('client_id','<>',$post['client_id'])->select();$data = json_encode(['style'=>'yue',//you'data' => $post['msg'],'name' => $name['name']]);$c_id = [];foreach ($cid as $v){$c_id[] = $v['client_id'];}Gateway::sendToAll($data,$c_id);return json($data);}public function close(Request $request){$post = $request->param();Db::name('user')->where('client_id',$post['Client_id'])->update(['client_id'=>'','status'=>0]);}}复制代码
后台代码就完成了
前端代码 js
<script src="js/vue.js"></script><script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script><script>// 创建一个webSocket对象const ws = new WebSocket("ws:192.168.3.116:8502");const token = {name:''};// 创建VUE对象const app = new Vue({el:"#app",data () {return {information:[],content:"",name:'',Client_id:0,};},created:function(){let that = this;ws.onmessage = e => {//console.log((new Date()).getTime());const receiveing = JSON.parse(e.data);if (receiveing.type=="start"){this.Client_id = receiveing.client_id;$.ajax({type: "post",url: "//192.168.3.122:83/api/Swoole/index",data: {Client_id:receiveing.client_id,},dataType: "json",success: function (data) {//console.log(3);that.information.push(data);that.name = data.name;}});}else {console.log(2);this.information.push(receiveing);console.log(this.information);}};ws.onclose = data => {// 监听连接关闭console.log("WebSocket连接已关闭");console.log((new Date()).getTime());$.ajax({type: "post",url: "//192.168.3.122:83/api/swoole/close",data: {Client_id:this.Client_id,},dataType: "json",success: function (data) {}});//console.log(data);};},methods:{send:function () {$.ajax({type: "post",url: "//192.168.3.122:83/api/swoole/send",data: {msg:this.content,client_id:this.Client_id,},dataType: "json",success: function (data) {//console.log(data.data);//that.information.push(data.data);}});this.information.push({style:'me',data:this.content,name:this.name});this.content="";},close:function(){ws.close();},heartbeat:function () {ws.send("1")},},beforeUpdate:function(){setInterval(this.heartbeat,14000);}})</script>复制代码
页面就自己写 基本功能出来了,剩下的根据自己的需求来改
最后
如果你觉得此文对你有一丁点帮助,点个赞。或者可以加入我的开发交流群:1025263163相互学习,我们会有专业的技术答疑解惑
如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点star:http://github.crmeb.net/u/defu不胜感激 !
PHP学习手册:https://doc.crmeb.com
技术交流论坛:https://q.crmeb.com