Home  >  Article  >  PHP Framework  >  How to configure workerman under Windows?

How to configure workerman under Windows?

青灯夜游
青灯夜游forward
2020-12-23 18:18:515037browse

How to configure workerman under Windows? The following article will introduce to you how to configure Workerman in Windows environment. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to configure workerman under Windows?

Related recommendations: "workerman Tutorial"

Note: Composer must be installed before installing workerman

1. The first step is to visit the official website to download Workerman. The URL is as follows: https://www.workerman.net/

2. After downloading and decompressing, I changed the folder name to "workerman", opened the directory, and created a new "start.php" file. The page is as follows

3. start.php The content is as follows:

<?php
 
use Workerman\Worker;
 
  
 
//Autoloader.php路径
 
require_once "./Autoloader.php";
 
  
 
$global_uid = 0;
 
  
 
// 当客户端连上来时分配uid,并保存连接,并通知所有客户端
 
function handle_connection($connection)
 
{
 
    global $text_worker, $global_uid;
 
    // 为这个连接分配一个uid
 
    $connection->uid = ++$global_uid;
 
}
 
  
 
// 当客户端发送消息过来时,转发给所有人
 
function handle_message($connection, $data)
 
{
 
    global $text_worker;
 
    foreach($text_worker->connections as $conn)
 
    {
 
        $conn->send("user[{$connection->uid}] said: $data");
 
    }
 
}
 
  
 
// 当客户端断开时,广播给所有客户端
 
function handle_close($connection)
 
{
 
    global $text_worker;
 
    foreach($text_worker->connections as $conn)
 
    {
 
        $conn->send("user[{$connection->uid}] logout");
 
    }
 
}
 
  
 
// 创建一个文本协议的Worker监听2000接口  用0.0.0.0方便链接内网外网
 
$text_worker = new Worker("websocket://0.0.0.0:2000");  
 
  
 
// 只启动1个进程,这样方便客户端之间传输数据
 
$text_worker->count = 1;
 
  
 
$text_worker->onConnect = &#39;handle_connection&#39;;
 
$text_worker->onMessage = &#39;handle_message&#39;;
 
$text_worker->onClose = &#39;handle_close&#39;;
 
  
 
Worker::runAll();

4. Place the workerman file in any disk. I put it in the root directory of drive D. The page is as follows:

5. Enter cmd to open the command panel and open the start.php file. The operation command and page are as follows:

1、D:
2、cd workerman
3、php start.php start

6. Whether the browser test is successful: open Google or Sogou In the browser (any browser), press the F12 key, open the console, enter the following content and press the Enter key. If a pop-up box appears, it indicates success.

ws = new WebSocket("ws://127.0.0.1:2000");
ws.onopen = function() {
    alert("连接成功");
    ws.send(&#39;tom&#39;);
    alert("给服务端发送一个字符串:tom");
};
ws.onmessage = function(e) {
    alert("收到服务端的消息:" + e.data);
};

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to configure workerman under Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete