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!