search
HomePHP FrameworkWorkermanHow does workerman work?

How does workerman work?

Dec 12, 2019 am 09:23 AM
workerman

How does workerman work?

First download workererman https://www.workerman.net/download                                                                                                                                                                                                                                    #After downloading, create a new file start.php under the workerman file

How does workerman work?

The code is as followsHow does workerman work?

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

The above is the detailed content of How does workerman work?. 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),