网站首页 > 精选文章 正文
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
猜你喜欢
- 2024-12-29 前端vue与后端Thinkphp在服务器的部署
- 2024-12-29 第03节:创建登陆页
- 2024-12-29 详解Nginx代理Vue3项目的实践与配置
- 2024-12-29 React/Vue路由全攻略:鉴权、导航守卫与拦截,让你站在技术之巅
- 2024-12-29 Vue3.2项目架构详解
- 2024-12-29 vue项目本地开发完成后部署到服务器后报404是什么原因呢?
- 2024-12-29 Vue路由配置方法详细介绍
- 2024-12-29 vue 路由守卫
- 2024-12-29 React 对比 Vue 如何做路由鉴权?
- 2024-12-29 vue3 动态组件使用技巧
- 最近发表
- 标签列表
- 
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)
 
