This article mainly introduces the Gatewayworker and thinkphp, such as the introduction of the framework, has a certain reference value, now share to everyone, the need for friends can refer to
What developers are most concerned about when using Gatewayworker is how to integrate with the existing MVC framework (thinkphp Yii laravel, etc.) and the following are the official recommended integrations.
See:
# # General principles
Existing MVC framework projects are not interfering with gatewayworker independent deployments
All business logic is done by the site page Post/get into the MVC framework
Gatewayworker does not accept data from clients , that is, Gatewayworker does not process any business logic ,gatewayworker only as a one-way push channel
Call the Gateway API (Gatewayclient) in the MVC framework to complete the push only when the MVC framework needs to proactively push data to the browser
# # Specific Implementation steps
1, the Site page establishment and GatewayworkeR 's websocket connection
2, Gatewayworker found that there is a page to initiate the connection , the corresponding connection client_id sent to the site page
3, the Site page received client_id after triggering an AJAX request (assuming that bind.php ) to send client_id to the MVC backend
4. After the MVC backend bind.php receives client_id , the gatewayclient Gateway::bindUid($client_id, $uid) call will client_id with the current UID( User ID or client unique identity) binding. If there is a group, mass function, you Gateway::joinGroup($client_id, $group_id) can also use to add client_id to the corresponding group
5. All requests initiated by the page are post/get directly to the MVC framework, including sending messages
6, the MVC framework processing business process needs to send data to a UID or a group , directly call the Gatewayclient interface Gateway::sendToUid Gateway::sendToGroup and so on to send
# # Sample Code
# # # Gatewayworker Fragment
events.php code in Gatewayworker (only one onconnect callback setting)
<?phpuse \gatewayworker\lib\gateway;class events{ //When there is a client connection, return client_id, let the MVC framework determine the current UID and execute the bind public static function OnConnect ($client _id) { gateway::sendtoclient ($client _id, Json_encode (Array ( ' type ' = ' init ', ' client_id ' = + $client _id))) ; Gatewayworker does not recommend any business logic, onMessage leave blank to public static function OnMessage ($client _id, $message) { }}
# # # Site page js fragment
/** * with Gatewayworker to establish WebSocket connection, domain name and port to your actual domain port, * where the port is the gateway port, that is, the start_gateway.php specified port. * start_gateway.php need to specify WebSocket protocol, like this * $gateway = new Gateway (websocket://0.0.0.0:7272); */ws = new WebSocket ("ws://your_domain.com:7272");//The server will trigger this onmessagews.onmessage = function (e) { // JSON data converted to JS object var data = eval ("(" +e.data+ ")"); var type = Data.type | | ''; Switch (type) { /////events.php returns the Init type of the message, and sends the client_id to the background for UID binding case ' init ': //using jquery to initiate an AJAX request, The client_id is sent to the backend for UID binding $.post ('./bind.php ', {client_id:data.client_id}, function (data) {}, ' json '); break; When the MVC framework invokes gatewayclient to send a message, the alert comes directly to the default: alert (e.data); }};
# # # MVC back-end UID binding code fragment
bind.php (binding with Gatewayclient)
<?php//load Gatewayclient. About Gatewayclient See the bottom of this page for require_once '/your/path/gatewayclient/gateway.php ';//gatewayclient 3.0. version 0 to start using the namespace use gatewayclient\gateway;//Set the Register service IP and port of the Gatewayworker service, please change the actual value (IP cannot be 0.0.0.0) according to the reality Gateway:: $registerAddress = ' 127.0.0.1:1236 ';//Assuming the user is logged in, User UID and group ID in SESSION $uid = $_session[' uid ']; $group _id = $_session[' group '];// CLIENT_ID and UID binding gateway::binduid ($client _id, $uid);//Join a group (can call multiple groups to join) Gateway::joingroup ($client _id, $group _id) ;
# # # MVC backend Send message code snippet
Send_message.php (Sent with gatewayclient)
<?php//load Gatewayclient. About Gatewayclient See the bottom of this page for require_once '/your/path/gatewayclient/gateway.php ';//gatewayclient 3.0. version 0 to start using the namespace use gatewayclient\gateway;//Set the Register service IP and port of the Gatewayworker service, please change the actual value (IP cannot be 0.0.0.0) according to the reality Gateway:: $registerAddress = ' 127.0.0.1:1236 ';//Send data to any UID Site page gateway::sendtouid ($uid, $message);//Send data to any group's web page Gateway::sendtogroup ($ Group, $message);
Attention
The above is only a combination of the MVC framework and the official recommendation of Gatewayworker, not the mandatory use of this approach, developers can freely change the choice of combination to adapt to their business needs.
Of course, you can also use the client and Gatewayworker direct two-way communication to complete the business communication.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!