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:247145browse

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 interfaceGateway::sendToUid Gateway::sendToGroup Wait for sending## Sample code

GatewayWorker fragment

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

 'init',            'client_id' => $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 || '';    switch(type){        // Events.php中返回的init类型的消息,将client_id发给后台进行uid绑定        case 'init':            // 利用jquery发起ajax请求,将client_id发给后端进行uid绑定
            $.post('./bind.php', {client_id: data.client_id}, function(data){}, 'json');            break;        // 当mvc框架调用GatewayClient发消息时直接alert出来        default :
            alert(e.data);
    }
};

mvc backend uid binding code fragment

bind.php (using GatewayClient binding)

mvc backend message code snippet

###send_message.php (sent using GatewayClient)###

注意

以上仅是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