关于php:ThinkPHP6和GatewayWorker简单的示例

36次阅读

共计 3763 个字符,预计需要花费 10 分钟才能阅读完成。

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

正文完
 0