Home PHP Framework Workerman How to configure workerman under Windows?

How to configure workerman under Windows?

Dec 23, 2020 pm 06:18 PM
windows workerman

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();
Copy after login

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
Copy after login

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);
};
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Beyond Compare fails to case sensitivity when synchronizing Windows and Linux files? What should I do if Beyond Compare fails to case sensitivity when synchronizing Windows and Linux files? Apr 01, 2025 am 08:06 AM

The problem of comparing and synchronizing BeyondCompare files: Case sensitivity failure when using Beyond...

Four ways to implement multithreading in C language Four ways to implement multithreading in C language Apr 03, 2025 pm 03:00 PM

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

How to avoid third-party interfaces returning 403 errors in Node environment? How to avoid third-party interfaces returning 403 errors in Node environment? Apr 01, 2025 pm 02:03 PM

How to avoid the third-party interface returning 403 error in the Node environment. When calling the third-party website interface using Node.js, you sometimes encounter the problem of returning 403 error. �...

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

Where to download Python .whl files under Windows? Where to download Python .whl files under Windows? Apr 01, 2025 pm 08:18 PM

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

How to make PHP script run in the background by adding parameters -d? How to make PHP script run in the background by adding parameters -d? Apr 01, 2025 pm 02:27 PM

How to make PHP scripts run in the background by adding parameters -d? When writing PHP scripts, sometimes you need to have the script run in the background instead of occupying the foreground...

How to efficiently read Windows system logs and get only information from the last few days? How to efficiently read Windows system logs and get only information from the last few days? Apr 01, 2025 pm 11:21 PM

Efficient reading of Windows system logs: Reversely traverse Evtx files When using Python to process Windows system log files (.evtx), direct reading will be from the earliest...

How to monitor system performance through Debian logs How to monitor system performance through Debian logs Apr 02, 2025 am 08:00 AM

Mastering Debian system log monitoring is the key to efficient operation and maintenance. It can help you understand the system's operating conditions in a timely manner, quickly locate faults, and optimize system performance. This article will introduce several commonly used monitoring methods and tools. Monitoring system resources with the sysstat toolkit The sysstat toolkit provides a series of powerful command line tools for collecting, analyzing and reporting various system resource metrics, including CPU load, memory usage, disk I/O, network throughput, etc. The main tools include: sar: a comprehensive system resource statistics tool, covering CPU, memory, disk, network, etc. iostat: disk and CPU statistics. mpstat: Statistics of multi-core CPUs. pidsta

See all articles