How TP5 integrates WorkerMan and GatewayWorker
The following column workerman tutorial will introduce to you how to integrate TP5 with WorkerMan and GatewayWorker. I hope it will be helpful to friends in need!
TP5 integrates GatewayWorker
Windows version installation
a) Use composer create -project topthink/think testTG to install thinkphp5.
b) Enter the directory of thinkphp5, here is testTG, and use composer require workerman/gateway-worker-for-win to install the Windows version of gateway.
c) Go to the official website to download the Windows version of gateway-worker, which contains a demo. http://www.workerman.net/download
d) Unzip the downloaded compressed package and copy all the files in Applications/Yourapp to any folder in the thinkphp5 directory application, here named push .
e) Copy start_for_win.bat in the decompressed folder to the root directory of thinkphp5, which is the directory at the same level as application.
f) Right-click start_for_win.bat, click Edit, change the directory inside to your own directory, here it is changed to
php application\push\start_register.php application\push\start_gateway.php application\push\start_businessworker.php Pause
g) Save and exit. Double click to run.
Linux version installation
a) Use composer create-project topthink/think testTG to install thinkphp5.
b) Enter the directory of thinkphp5, Here is testTG. Use composer require workerman/gateway-worker to install the Linux version of gateway.
c) Go to the official website to download the Linux version of gateway-worker, which contains a demo. http://www.workerman.net/download
d) Unzip the downloaded compressed package and copy all the files in Applications/Yourapp to any folder in the thinkphp5 directory application, here named push .
e) Copy start.php in the decompressed folder to the root directory of thinkphp5, which is a directory at the same level as application.
f) Change the path in the forearch loop brackets in the last part of the start.php file to your correct path.
Start on the command line php start.php start.
TP5 integrationWrokerMan
Windows version installation
a) Use composer create-project topthink /think testTW to install thinkphp5.
b) Enter the thinkphp5 root directory, which is testTW. First use composer require topthink/think-worker,
and then use composer require workerman/workerman-for-win to install workererman. After successful installation, delete vendor\workerman\workerman.
c) Create server.php in the thinkphp5 root directory (that is, the directory at the same level as application) and edit the content.
<?php efine('APP_PATH', __DIR__ . '/application/'); define('BIND_MODULE','push/Worker'); // 加载框架引导文件 require __DIR__ . '/thinkphp/start.php';
d) Create workerman’s controller and name it Worker.php. In application/push/controller, the directory does not exist and is created by itself. Add the following content:
protected $socket = 'websocket://127.0.0.1:2346' where 127.0.0.1 is the IP address of the socket server. Listen here to port 2346 of this machine.
<?php namespace app\push\controller; use think\worker\Server; class Worker extends Server { protected $socket = 'websocket://127.0.0.1:2346'; /** * 收到信息 * @param $connection * @param $data */ public function onMessage($connection, $data) { $connection->send('我收到你的信息了'); } /** * 当连接建立时触发的回调函数 * @param $connection */ public function onConnect($connection) { } /** * 当连接断开时触发的回调函数 * @param $connection */ public function onClose($connection) { } /** * 当客户端的连接上发生错误时触发 * @param $connection * @param $code * @param $msg */ public function onError($connection, $code, $msg) { echo "error $code $msg\n"; } /** * 每个进程启动 * @param $worker */ public function onWorkerStart($worker) { } }
e) Run from the command line and start the listening service php server.php
f) Create a new html file at any location. The content is:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script> ws = new WebSocket("ws://localhost:2346"); ws.onopen = function() { alert("连接成功"); ws.send('tom'); alert("给服务端发送一个字符串:tom"); }; ws.onmessage = function(e) { alert("收到服务端的消息:" + e.data); }; </script>
g) Save it and open it with a browser, you can see that the link is successful.
Linux version installation
a) Just execute the composer command in step b) of Windows version installation: composer require topthink/think-worker. That’s it, the rest of the steps remain unchanged.
Simple use of GatewayWorker
<script type="text/javascript"> /** * 与GatewayWorker建立websocket连接,域名和端口改为你实际的域名端口, * 其中端口为Gateway端口,即start_gateway.php指定的端口。 * start_gateway.php 中需要指定websocket协议,像这样 * $gateway = new Gateway(websocket://0.0.0.0:7272); */ ws = new WebSocket("ws://127.0.0.1:8282"); // 服务端主动推送消息时会触发这里的onmessage ws.onmessage = function(e){ // json数据转换成js对象 var bindUrl = "{:url('push/BindClientId')}"; var data = e.data; var type = data.type || ''; switch(type){ // Events.php中返回的init类型的消息,将client_id发给后台进行uid绑定 case 'init': // 利用jquery发起ajax请求,将client_id发给后端进行uid绑定 $.post(bindUrl, {client_id: data.client_id}, function(data){ }, 'json'); break; // 当mvc框架调用GatewayClient发消息时直接alert出来 default : var text = e.data; var str = '<li style="width:100%; height:60px; border:1px solid #000">' +text +'</li>'; $('#message_box').append(); // alert(e.data); } }; </script> class Push{ public function helloAction () { $uid = $_GET['uid']; session('uid', $uid); $view = new View; return $view->fetch(); } public function BindClientIdAction () { $client_id = $_POST['client_id']; // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值 Gateway::$registerAddress = '127.0.0.1:1238'; $bindUid = session('uid'); // 假设用户已经登录,用户uid和群组id在session中 // client_id与uid绑定 Gateway::bindUid($client_id, $bindUid); // 加入某个群组(可调用多次加入多个群组) // Gateway::joinGroup($client_id, $group_id); } public function AjaxSendMessageAction () { $message = $_POST['message']; // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值 Gateway::$registerAddress = '127.0.0.1:1238'; GateWay::sendToAll($message); } }
The above is the detailed content of How TP5 integrates WorkerMan and GatewayWorker. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

Introduction to how to implement the basic usage of Workerman documents: Workerman is a high-performance PHP development framework that can help developers easily build high-concurrency network applications. This article will introduce the basic usage of Workerman, including installation and configuration, creating services and listening ports, handling client requests, etc. And give corresponding code examples. 1. Install and configure Workerman. Enter the following command on the command line to install Workerman: c

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

Workerman development: real-time video call based on UDP protocol Summary: This article will introduce how to use the Workerman framework to implement real-time video call function based on UDP protocol. We will have an in-depth understanding of the characteristics of the UDP protocol and show how to build a simple but complete real-time video call application through code examples. Introduction: In network communication, real-time video calling is a very important function. The traditional TCP protocol may have problems such as transmission delays when implementing high-real-time video calls. And UDP

How to implement the timer function in the Workerman document Workerman is a powerful PHP asynchronous network communication framework that provides a wealth of functions, including the timer function. Use timers to execute code within specified time intervals, which is very suitable for application scenarios such as scheduled tasks and polling. Next, I will introduce in detail how to implement the timer function in Workerman and provide specific code examples. Step 1: Install Workerman First, we need to install Worker

How to use Workerman to build a high-availability load balancing system requires specific code examples. In the field of modern technology, with the rapid development of the Internet, more and more websites and applications need to handle a large number of concurrent requests. In order to achieve high availability and high performance, the load balancing system has become one of the essential components. This article will introduce how to use the PHP open source framework Workerman to build a high-availability load balancing system and provide specific code examples. 1. Introduction to Workerman Worke

How to implement the reverse proxy function in the Workerman document requires specific code examples. Introduction: Workerman is a high-performance PHP multi-process network communication framework that provides rich functions and powerful performance and is widely used in Web real-time communication and long connections. Service scenarios. Among them, Workerman also supports the reverse proxy function, which can realize load balancing and static resource caching when the server provides external services. This article will introduce how to use Workerman to implement the reverse proxy function.

How to implement TCP/UDP communication in the Workerman document requires specific code examples. Workerman is a high-performance PHP asynchronous event-driven framework that is widely used to implement TCP and UDP communication. This article will introduce how to use Workerman to implement TCP and UDP-based communication and provide corresponding code examples. 1. Create a TCP server for TCP communication. It is very simple to create a TCP server using Workerman. You only need to write the following code: <?ph
