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
workerman和swoole性能谁更好?如何选择?workerman和swoole性能谁更好?如何选择?Dec 01, 2022 am 10:00 AM

workerman 对比 swoole 实际开发项目中,你会选择哪个?对于新手学哪个较好,有什么建议吗?

如何利用Workerman实现PHP和Unity3D的跨平台游戏联机功能如何利用Workerman实现PHP和Unity3D的跨平台游戏联机功能Jul 17, 2023 am 10:21 AM

如何利用Workerman实现PHP和Unity3D的跨平台游戏联机功能随着移动游戏的兴起,跨平台游戏联机功能成为游戏开发者关注的焦点之一。PHP作为一种广泛应用于Web开发的语言,而Unity3D作为一款强大的跨平台游戏引擎,如何实现二者之间的联机功能成为了开发者们思考的问题。本文将介绍如何利用Workerman实现PHP和Unity3D的跨平台游戏联机功

如何利用PHP和Unity3D开发基于Workerman的实时多人游戏如何利用PHP和Unity3D开发基于Workerman的实时多人游戏Jul 18, 2023 am 09:54 AM

如何利用PHP和Unity3D开发基于Workerman的实时多人游戏随着游戏行业的不断发展,实时多人游戏成为了一种趋势。而PHP作为一种广泛使用的服务器端脚本语言和Unity3D作为一种流行的游戏开发引擎,如果能够结合起来开发实时多人游戏,将会带来更加丰富的玩法和用户体验。本文将详细介绍如何利用PHP和Unity3D开发基于Workerman的实时多人游戏

PHP和Unity3D如何利用Workerman实现服务器端推送功能PHP和Unity3D如何利用Workerman实现服务器端推送功能Jul 18, 2023 pm 12:01 PM

PHP和Unity3D如何利用Workerman实现服务器端推送功能在现代的网络应用中,服务器端推送功能(ServerPush)显示了它的强大威力。它可以实时地将信息推送给客户端,而无需客户端不停地向服务器发起请求。在本文中,我们将讨论如何使用PHP和Unity3D结合使用Workerman框架来实现服务器端推送功能。Workerman是一个使用纯PHP编

如何使用Workerman实现PHP和Unity3D的数据统计和分析功能如何使用Workerman实现PHP和Unity3D的数据统计和分析功能Jul 16, 2023 pm 11:43 PM

如何使用Workerman实现PHP和Unity3D的数据统计和分析功能引言:随着互联网的快速发展,数据统计和分析变得愈发重要。在PHP和Unity3D开发过程中,我们经常需要收集和分析用户的行为数据,以便进行产品改进和决策制定。本文将介绍如何使用Workerman这个高性能的PHP开发框架实现PHP和Unity3D之间的数据统计和分析功能。一、Worker

如何使用Workerman实现PHP和Unity3D的多人协同编辑功能如何使用Workerman实现PHP和Unity3D的多人协同编辑功能Jul 17, 2023 pm 04:03 PM

如何使用Workerman实现PHP和Unity3D的多人协同编辑功能引言:在现如今的互联网时代,多人协同编辑已经成为一个非常重要和常见的功能需求。无论是团队合作中的文档编辑,还是多人在线游戏中的场景编辑,都需要实现多人同时编辑同一个文件或场景的功能。本文将介绍如何使用Workerman框架实现PHP和Unity3D的多人协同编辑功能,并提供代码示例。一、什

如何使用Workerman实现PHP和Unity3D的多人在线拼图游戏如何使用Workerman实现PHP和Unity3D的多人在线拼图游戏Jul 17, 2023 pm 10:55 PM

如何使用Workerman实现PHP和Unity3D的多人在线拼图游戏概述:多人在线游戏一直是游戏开发领域的一个热门话题,而拼图游戏作为一种简单、有趣的休闲游戏,也在线上游戏中广受欢迎。本文将介绍如何使用Workerman搭建服务器,并使用PHP和Unity3D开发一个简单的多人在线拼图游戏,实现实时的游戏互动。搭建服务器首先,我们需要搭建一个服务器来提供网

聊聊Laravel中怎么接入workerman聊聊Laravel中怎么接入workermanDec 07, 2022 pm 07:21 PM

laravel+websocket是即时通讯开发必备利器,那么Laravel中怎么接入workerman​?下面本篇文章给大家介绍一下laravel快速接入websocket的方法,希望对大家有所帮助。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!