thinkphp6 使用GatewayWorker和workerman

折腾了好久,终于知道怎么结合到thinkphp6中用了! 由于php think worker:gateway命令在windows使用会报错GatewayWorker Not Support On Windows.,所有就用虚拟机搞了,centos7.

1.think-worker安装  参考手册:https://www.kancloud.cn/manual/thinkphp6_0/1147857(手册有说怎么使用workman了,就是没说GatewayWorker)

composer require topthink/think-worker

2.think-worker默认是安装了workman和GatewayWorker了,在vendor/workman目录下

3.webcocket事例代码

html:

<!DOCTYPE html>  
<html>  
<head>  
<title>HTML5</title>  
<meta charset="utf-8" />  
<body>

       <!-- <a href="javascipt:" class="send">发送</a>  -->

</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>  
<script>  

// $('.send').click(function(){
//     $.ajax({
//         type: 'POST',
//         url: "{:url('index/index/send')}",
//         data: {'id':1},
//         dataType: 'json',
//         success: function (data) {
//             if (data.status == 1) {
//                 alert('发送成功')
//                 // layer.msg(data.msg, { icon: 1, time: 1000 },function(){
//                 //     // parent.layer.close(index);
//                 //     window.location.reload()
//                 //     // parent.location.reload();
//                 // });
//             } else {
//                 alert('发送失败')
//                 // layer.msg(data.msg, { icon: 2, time: 1000 });
//             }
//         },
//         error: function () {
//             // layer.alert("服务器繁忙, 请联系管理员!");
//         },
//     });    
// })

$(function() {      
    var socket;  
    var readyState = ["connecting", "connected", "closing", "closed"];  
    /* 打开连接事件 */  
    $("button:eq(0)").click(function() {  
        try {  
             /* 连接 */  
            //  socket = new WebSocket("ws://39.96.9.241:6789");  
             socket = new WebSocket("ws://192.168.30.128:2348");
             /* 绑定事件 */  
             socket.onopen = function() {  
                 $("#msg").html("连接成功...");  
             };  
               
            socket.onmessage = function(e) {  
                 $("#msg").html($("#msg").html() + "<br />" + e.data);  
             };  
               
             socket.onclose = function() {  
                 $("#msg").html($("#msg").html() + "<br />关闭连接...");  
             };  
        } catch(exception) {  
            $("#msg").html($("#msg").html() + "<br />有错误发生");  
        }  
    });  
      
    /* 发送数据事件 */  
    $("button:eq(1)").click(function() {  
        /* 检查文本框是否为空 */  
        if($("#data").val() == "") {  
            alert("请输入数据!");  
            return;  
        }  
          
        try {  
            socket.send($("#data").val());  
            $("#msg").html($("#msg").html() + "<br />发送数据:" + $("#data").val());  
        } catch (exception) {  
            $("#msg").html($("#msg").html() + "<br />发送数据出错");  
        }  
          
        /* 清空文本框 */  
        $("#data").val("");  
    });  
      
    /* 断开连接 */  
    $("button:eq(2)").click(function() {  
        socket.close();  
    });  
});  
</script>  
</head>  
  
<body>  
<h1>WebSocket示例</h1>  
<input type="text" id="data" />  
<button>打开连接</button>  
<button>发送数据</button>  
<button>关闭连接</button>  
<p id="msg"></p>  
</body>  
</html>

4.执行php think worker:gateway

thinkphp6在centos中启动gatewayworker报错Uncaught Error: Call to undefined function posix_getpid();

解决方法:

yum install php-posix

执行这个就不用执行启动workman的命令了

5.

6.Events的代码在哪里?

在config/gateway_worker.php配置信息

下面是events代码

ps:如果开启了gatewayworker,websocket没反应,则是防火墙没开放对应的端口

猜你喜欢

转载自blog.csdn.net/lmp5023/article/details/106647377