search
HomePHP FrameworkWorkermanHow does workerman count the number of people online?

How does workerman count the number of people online?

workerman statistics online number implementation code: (recommended: workerman tutorial)

Server-side code

<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
require_once __DIR__ . &#39;/Workerman/Autoloader.php&#39;;

$worker = new Worker(&#39;websocket://127.0.0.1:2345&#39;);
// 全局变量,保存当前进程的客户端连接数
$connection_count = 0;

// 这个例子中进程数必须为1
$worker->count = 1;
$worker->onConnect = function($connection)
{
    // 有新的客户端连接时,连接数+1
    global $connection_count;
    
    ++$connection_count;
    echo "now connection_count=$connection_count\n";
};
// 进程启动时设置一个定时器,定时向所有客户端连接发送数据
$worker->onWorkerStart = function($worker)
{
    // 定时,每10秒一次
    Timer::add(1, function()use($worker)
    {
        global $connection_count;
        // 遍历当前进程所有的客户端连接,发送当前服务器的时间
        foreach($worker->connections as $connection)
        {
            $connection->send($connection_count);
        }
    });
};
$worker->onClose = function($connection)
{
    // 客户端关闭时,连接数-1
    global $connection_count;
    $connection_count--;
    echo "now connection_count=$connection_count\n";
};
// 运行worker
Worker::runAll();

When the client The callback function triggered when a connection is established with Workerman (after the TCP three-way handshake is completed). The onConnect callback will only be triggered once per connection.

The callback function triggered when the client connection is disconnected from Workerman. No matter how the connection is disconnected, onClose will be triggered as soon as it is disconnected. onClose will only be triggered once per connection.

Client code

<?php

?>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
 function ds(){
		$.get(&#39;127.0.0.1:2345&#39;,function(data,status){
			console.log("Data: " + data + "nStatus: " + status);
			})
	 }
// window.setInterval(ds,1000);

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

</script>

The above is the detailed content of How does workerman count the number of people online?. 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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool