GatewayWorker在windows上的使用

记录一次使用GatewayWorker-for-windows使用

GatewayWorker在windows上使用

第一步准备好最新的TP5和GatewayWorker
https://www.workerman.net/download //GatewayWorker下载地址
http://www.thinkphp.cn/down/1278.html //TP5下载地址
下载完成解压到本地。
GatewayWorker直接放到TP5根目录下就行

下载GatewayClient

https://github.com/walkor/GatewayClient //下载地址

因为是最新版的GatewayWorker直接下载最新版的GatewayClient就行,然后解压里面的Gateway.php

放到TP5下的E:\WWW\tp5\thinkphp\library\think这是我的目录,前面要改成自己的目录。

第二步修改内容
修改刚刚解压的Gateway.php里的内容
namespace GatewayClient; 修改成
namespace think;

把 注册中心地址改成 public static $registerAddress = ‘127.0.0.1:1238’;

记住后面端口一定要和GatewayWorker\Applications\YourApp下的start_businessworker.php,start_businessworker.php,start_register.php保持一致就行,因为那三个默认是1238,我比较懒我就改成了1238

修改GatewayWorker\Applications\YourApp下的start_gateway.php

$gateway = new Gateway(“tcp://0.0.0.0:8282”);
修改成
$gateway = new Gateway(“websocket://0.0.0.0:8282”);

第三步创建控制器
话不多说直接贴上控制器代码
————————————————

<?php
namespace app\index\controller;
 
use think\Controller;
use think\Gateway;
class Index extends Controller
{
    public function index()
    {
 
        $this->uid = input('uid');
 
        session('uid', $this->uid);
        return $this->fetch();
    }
    function bind() {
        $uid = session('uid');
        $client_id = input('client_id');
 
        $gateway = new Gateway();
 
        $gateway->bindUid($client_id, $uid);
        $message = '绑定成功' . $uid . '-' . $client_id;
        $gateway->sendToUid($uid,$message);
    }
 
    function message() {
        $to_uid = input('uid');
        $message = input('msg');
        $gateway = new Gateway();
        $data['msg'] = $message;
        $data['from_uid'] = session('uid');
        $data['to_uid'] = $to_uid;
        $gateway->sendToUid($to_uid, json_encode($data)); //发给对方
// $gateway->sendToUid($data['from_uid'], json_encode($data)); //发给自己
        echo json_encode($data);
    }
}

新建index.html,代码如下

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>chatroom</title>
    <script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"/></script>
 
    <script type="text/javascript">
    // 打开一个 web socket
    var ws = new WebSocket("ws://127.0.0.8:8282");
 
    ws.onopen = function ()
    {
        console.log('上线');
    };
    ws.onerror=function(e){
        console.log(e);
    }
    ws.onclose=function(e){
        console.log('下线');
    }
    ws.onmessage = function (evt)
    {
        var received_msg = evt.data;
        alert("数据已接收..." + received_msg);
        if(isJsonString(received_msg)){
            var jmsg = JSON.parse(received_msg);
        }else{
            var jmsg = received_msg;
        }
 
        console.log(jmsg);
        // console.log();
        if (jmsg.from_uid > 0) {
 
            var t_msg = '<li>' + jmsg.from_uid + ' 说:' + jmsg.msg + '</li>';
 
            $("#message").append(t_msg);
        }
 
        if ('client_id' in jmsg && jmsg.client_id.length != 0) {
            $.post("{:url('bind')}", {client_id: jmsg.client_id}, function (data) {
                // console.log(data);
            });
        }
    }
 
    function isJsonString(str) {
        try {
            if (typeof JSON.parse(str) == "object") {
                return true;
            }
        } catch (e) {
        }
        return false;
    }
    </script>
</head>
<body>
<ul id="message">
 
</ul>
 
<div>
    <input name="uid" value="1" >
    <input name="msg" value="" size="50">
    <button type="button" id="send" >发送</button>
</div>
 
<script>
    $(function () {
        $("#send").click(function () {
            var uid = $('input[name="uid"]').val();
            var msg = $('input[name="msg"]').val();
            $.post("{:url('message')}", {uid: uid, msg: msg}, function (data) {
                $('input[name="msg"]').val('');
            });
 
        });
    });
</script>
</body>
</html>

然后在本地服务器开启GatewayWorker

双击打开GatewayWorker\start_for_win.bat

出现这个画面证明开启成功,如果报php错误说明你的php没有加到环境变量里,百度一下php怎么加入环境变量

打开浏览器输入网址加上Uid

出现绑定成功说明已经成功了,然后第一个输入框输入对应id就能和这个id的人对话了

没有账号? 忘记密码?

社交账号快速登录