search
HomePHP FrameworkWorkermanHow to download workerman

How to download workerman

Dec 02, 2019 am 09:31 AM
workerman

How to download workerman

First download workerman https://www.workerman.net/download (Recommended learning: workerman tutorial)

How to download workerman

After downloading, create a new file start.php under the workerman file

How to download workerman

##The code 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();

Then run the command line php start.php start

Simple test is to open the browser and press F12 to open it Debug the console, enter in the Console column (or put the following code into the html page and run it with jsHow to download workerman

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

How to download workerman

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

Statement
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
What Are the Key Features of Workerman's Built-in WebSocket Client?What Are the Key Features of Workerman's Built-in WebSocket Client?Mar 18, 2025 pm 04:20 PM

Workerman's WebSocket client enhances real-time communication with features like asynchronous communication, high performance, scalability, and security, easily integrating with existing systems.

How to Use Workerman for Building Real-Time Collaboration Tools?How to Use Workerman for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:15 PM

The article discusses using Workerman, a high-performance PHP server, to build real-time collaboration tools. It covers installation, server setup, real-time feature implementation, and integration with existing systems, emphasizing Workerman's key f

What Are the Best Ways to Optimize Workerman for Low-Latency Applications?What Are the Best Ways to Optimize Workerman for Low-Latency Applications?Mar 18, 2025 pm 04:14 PM

The article discusses optimizing Workerman for low-latency applications, focusing on asynchronous programming, network configuration, resource management, data transfer minimization, load balancing, and regular updates.

How to Implement Real-Time Data Synchronization with Workerman and MySQL?How to Implement Real-Time Data Synchronization with Workerman and MySQL?Mar 18, 2025 pm 04:13 PM

The article discusses implementing real-time data synchronization using Workerman and MySQL, focusing on setup, best practices, ensuring data consistency, and addressing common challenges.

What Are the Key Considerations for Using Workerman in a Serverless Architecture?What Are the Key Considerations for Using Workerman in a Serverless Architecture?Mar 18, 2025 pm 04:12 PM

The article discusses integrating Workerman into serverless architectures, focusing on scalability, statelessness, cold starts, resource management, and integration complexity. Workerman enhances performance through high concurrency, reduced cold sta

How to Build a High-Performance E-Commerce Platform with Workerman?How to Build a High-Performance E-Commerce Platform with Workerman?Mar 18, 2025 pm 04:11 PM

The article discusses building a high-performance e-commerce platform using Workerman, focusing on its features like WebSocket support and scalability to enhance real-time interactions and efficiency.

What Are the Advanced Features of Workerman's WebSocket Server?What Are the Advanced Features of Workerman's WebSocket Server?Mar 18, 2025 pm 04:08 PM

Workerman's WebSocket server enhances real-time communication with features like scalability, low latency, and security measures against common threats.

How to Use Workerman for Building Real-Time Analytics Dashboards?How to Use Workerman for Building Real-Time Analytics Dashboards?Mar 18, 2025 pm 04:07 PM

The article discusses using Workerman, a high-performance PHP server, to build real-time analytics dashboards. It covers installation, server setup, data processing, and frontend integration with frameworks like React, Vue.js, and Angular. Key featur

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.