Home  >  Article  >  Backend Development  >  Introduction to the combination of GatewayWorker and ThinkPHP and other frameworks

Introduction to the combination of GatewayWorker and ThinkPHP and other frameworks

不言
不言Original
2018-07-05 14:59:247201browse

This article mainly introduces the combination of GatewayWorker and ThinkPHP and other frameworks. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Developers are most concerned about when using GatewayWorker The most important thing is how to integrate with the existing mvc framework (ThinkPHP Yii laravel, etc.). The following is the officially recommended integration method.

See diagram:

## Overall principles

Existingmvc framework projectWithGatewayWorkerIndependentDeployment does not interfere with each other

All business logic is completed from the website page post/get to the mvc framework

##GatewayWorker Does not accept data from the client, that is, GatewayWorkerdoes not process any business logic,GatewayWorker only serves as A one-way push channel

Only ifmvc frameworkNeedActively push data to the browserOnly in the mvc frameworkCall Gateway's API (GatewayClient)Complete the push

## Specific implementation steps

1.Website pageEstablishment andGatewayWorker#websocket connection

2. GatewayWorker found that a page initiated the connection When , send the client_id corresponding to the connection to the website page

3, Website page triggers an ajax request after receiving client_id (assuming it is bind.php)Send client_id to mvc backend

##4、mvc backend

bind.php After receiving client_id, use GatewayClient to call Gateway::bindUid($client_id, $uid)Bind client_id to the current uid (user id or client unique identifier). If there is a group or group sending function, you can also use Gateway::joinGroup($client_id, $group_id) to add client_id to the corresponding group

5. All requests initiated by the page are directly post/get to the mvc framework for unified processing, including sending messages

6. During the business process of the mvc framework

When you need to send data to a uid or a group, directly call the GatewayClient interface<span style="color: #ff0000;"><strong>Gateway::sendToUid<span style="text-decoration: underline;"><span style="color: #000000; text-decoration: underline;"> </span></span></strong>Gateway::sendToGroup<span style="text-decoration: underline; color: #000000;"><strong></strong></span></span> Wait for sending## Sample code

GatewayWorker fragment

Events.php code in GatewayWorker (only one onConnect callback setting)

<?php
use \GatewayWorker\Lib\Gateway;class Events
{    // 当有客户端连接时,将client_id返回,让mvc框架判断当前uid并执行绑定    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id, json_encode(array(            &#39;type&#39;      => &#39;init&#39;,            &#39;client_id&#39; => $client_id
        )));
    }    // GatewayWorker建议不做任何业务逻辑,onMessage留空即可    public static function onMessage($client_id, $message)
    {

    }
}

Website page js fragment

/**
 * 与GatewayWorker建立websocket连接,域名和端口改为你实际的域名端口,
 * 其中端口为Gateway端口,即start_gateway.php指定的端口。
 * start_gateway.php 中需要指定websocket协议,像这样
 * $gateway = new Gateway(websocket://0.0.0.0:7272);
 */
ws = new WebSocket("ws://your_domain.com:7272");// 服务端主动推送消息时会触发这里的onmessage
ws.onmessage = function(e){    // json数据转换成js对象    var data = eval("("+e.data+")");    var type = data.type || &#39;&#39;;    switch(type){        // Events.php中返回的init类型的消息,将client_id发给后台进行uid绑定        case &#39;init&#39;:            // 利用jquery发起ajax请求,将client_id发给后端进行uid绑定
            $.post(&#39;./bind.php&#39;, {client_id: data.client_id}, function(data){}, &#39;json&#39;);            break;        // 当mvc框架调用GatewayClient发消息时直接alert出来        default :
            alert(e.data);
    }
};

mvc backend uid binding code fragment

bind.php (using GatewayClient binding)

<?php//加载GatewayClient。关于GatewayClient参见本页面底部介绍require_once &#39;/your/path/GatewayClient/Gateway.php&#39;;// GatewayClient 3.0.0版本开始要使用命名空间use GatewayClient\Gateway;// 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
Gateway::$registerAddress = &#39;127.0.0.1:1236&#39;;// 假设用户已经登录,用户uid和群组id在session中
$uid      = $_SESSION[&#39;uid&#39;];
$group_id = $_SESSION[&#39;group&#39;];// client_id与uid绑定
Gateway::bindUid($client_id, $uid);// 加入某个群组(可调用多次加入多个群组)
Gateway::joinGroup($client_id, $group_id);

mvc backend message code snippet

###send_message.php (sent using GatewayClient)###
<?php//加载GatewayClient。关于GatewayClient参见本页面底部介绍require_once &#39;/your/path/GatewayClient/Gateway.php&#39;;// GatewayClient 3.0.0版本开始要使用命名空间use GatewayClient\Gateway;// 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
Gateway::$registerAddress = &#39;127.0.0.1:1236&#39;;// 向任意uid的网站页面发送数据
Gateway::sendToUid($uid, $message);// 向任意群组的网站页面发送数据
Gateway::sendToGroup($group, $message);

注意

以上仅是mvc框架与GatewayWorker官方推荐的结合方式,并不是强制使用此方式,开发者可以自由变化选择结合方式以适应自己的业务需求。


当然也可以采用客户端与GatewayWorker直接双向通讯的方式完成业务通讯。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

Yii1.1框架关于日志配置的使用

用laravel dingo api插件库创建api的方法

The above is the detailed content of Introduction to the combination of GatewayWorker and ThinkPHP and other frameworks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn