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'); // 加載框架引導(dǎo)文件 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('我收到你的信息了'); } /** * 當(dāng)連接建立時觸發(fā)的回調(diào)函數(shù) * @param $connection */ public function onConnect($connection) { } /** * 當(dāng)連接斷開時觸發(fā)的回調(diào)函數(shù) * @param $connection */ public function onClose($connection) { } /** * 當(dāng)客戶端的連接上發(fā)生錯誤時觸發(fā) * @param $connection * @param $code * @param $msg */ public function onError($connection, $code, $msg) { echo "error $code $msg\n"; } /** * 每個進(jì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("給服務(wù)端發(fā)送一個字符串:tom"); }; ws.onmessage = function(e) { alert("收到服務(wù)端的消息:" + 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協(xié)議,像這樣 * $gateway = new Gateway(websocket://0.0.0.0:7272); */ ws = new WebSocket("ws://127.0.0.1:8282"); // 服務(wù)端主動推送消息時會觸發(fā)這里的onmessage ws.onmessage = function(e){ // json數(shù)據(jù)轉(zhuǎn)換成js對象 var bindUrl = "{:url('push/BindClientId')}"; var data = e.data; var type = data.type || ''; switch(type){ // Events.php中返回的init類型的消息,將client_id發(fā)給后臺進(jìn)行uid綁定 case 'init': // 利用jquery發(fā)起ajax請求,將client_id發(fā)給后端進(jìn)行uid綁定 $.post(bindUrl, {client_id: data.client_id}, function(data){ }, 'json'); break; // 當(dāng)mvc框架調(diào)用GatewayClient發(fā)消息時直接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']; // 設(shè)置GatewayWorker服務(wù)的Register服務(wù)ip和端口,請根據(jù)實際情況改成實際值 Gateway::$registerAddress = '127.0.0.1:1238'; $bindUid = session('uid'); // 假設(shè)用戶已經(jīng)登錄,用戶uid和群組id在session中 // client_id與uid綁定 Gateway::bindUid($client_id, $bindUid); // 加入某個群組(可調(diào)用多次加入多個群組) // Gateway::joinGroup($client_id, $group_id); } public function AjaxSendMessageAction () { $message = $_POST['message']; // 設(shè)置GatewayWorker服務(wù)的Register服務(wù)ip和端口,請根據(jù)實際情況改成實際值 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

Undress AI Tool
Undress images for free

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

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

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)

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

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.

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

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 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.

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 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

Workerman is a high-performance asynchronous event-driven framework developed based on PHP. It can easily realize the development of long connections under the TCP/UDP protocol. In addition, Workerman also provides the function of realizing file transfer, which can be used in scenarios such as large file transfer and data backup. This article will introduce how to implement the file transfer function in Workerman and provide specific code examples. 1. File upload function To implement the file upload function, the client needs to send the file to be uploaded to the server, and the server verifies
