基于GatewayWorker实现用户实时聊天

基于GatewayWorker实现用户实时聊天Workerman What is it Workerman is an asynchronous event driven PHP framework with high performance to build fast and scalable network applications Workerman supports HTTP Websocket SSL and

大家好,我是讯享网,很高兴认识大家。这里提供最前沿的Ai技术和互联网信息。

# Workerman Gitter

What is it

Workerman is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications. Workerman supports HTTP, Websocket, SSL and other custom protocols. Workerman supports event extension.

Requires

Installation

composer require workerman/workerman 

Basic Usage

A websocket server

 
  
    
    onConnect = function ($connection) { echo "New connection "; }; // Emitted when data received $ws_worker->onMessage = function ($connection, $data) { // Send hello $data $connection->send('Hello ' . $data); }; // Emitted when connection closed $ws_worker->onClose = function ($connection) { echo "Connection closed "; }; // Run worker Worker::runAll(); 

An http server

 
  
    
    count = 4; // Emitted when data received $http_worker->onMessage = function ($connection, $request) ; // Run all workers Worker::runAll(); 

A tcp server

 
  
    
    count = 4; // Emitted when new connection come $tcp_worker->onConnect = function ($connection) { echo "New Connection "; }; // Emitted when data received $tcp_worker->onMessage = function ($connection, $data) { // Send data to client $connection->send("Hello $data "); }; // Emitted when connection is closed $tcp_worker->onClose = function ($connection) { echo "Connection closed "; }; Worker::runAll(); 

A udp server

 
  
    
    count = 4; // Emitted when data received $worker->onMessage = function($connection, $data) { $connection->send($data); }; Worker::runAll(); 

Enable SSL

 
  
    
     array( 'local_cert' => '/your/path/of/server.pem', 'local_pk' => '/your/path/of/server.key', 'verify_peer' => false, ) ); // Create a Websocket server with ssl context. $ws_worker = new Worker('websocket://0.0.0.0:2346', $context); // Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). // The similar approaches for Https etc. $ws_worker->transport = 'ssl'; $ws_worker->onMessage = function ($connection, $data) { // Send hello $data $connection->send('Hello ' . $data); }; Worker::runAll(); 

Custom protocol

Protocols/MyTextProtocol.php

 
  
    
    
 
   
     
     onConnect = function ($connection) { echo "New connection "; }; $text_worker->onMessage = function ($connection, $data) { // Send data to client $connection->send("Hello world "); }; $text_worker->onClose = function ($connection) { echo "Connection closed "; }; // Run all workers Worker::runAll(); 

Timer

 
   
     
     onWorkerStart = function ($task) { // 2.5 seconds $time_interval = 2.5; $timer_id = Timer::add($time_interval, function () { echo "Timer run "; }); }; // Run all workers Worker::runAll(); 

AsyncTcpConnection (tcp/ws/text/frame etc…)

 
   
     
     onWorkerStart = function () { // Websocket protocol for client. $ws_connection = new AsyncTcpConnection('ws://echo.websocket.org:80'); $ws_connection->onConnect = function ($connection) { $connection->send('Hello'); }; $ws_connection->onMessage = function ($connection, $data) { echo "Recv: $data "; }; $ws_connection->onError = function ($connection, $code, $msg) { echo "Error: $msg "; }; $ws_connection->onClose = function ($connection) { echo "Connection closed "; }; $ws_connection->connect(); }; Worker::runAll(); 

Available commands

php start.php start
php start.php start -d
workerman start
php start.php status
workerman satus
php start.php connections
php start.php stop
php start.php restart
php start.php reload




















































Documentation

中文主页:http://www.workerman.net

中文文档: https://www.workerman.net/doc/workerman

Documentation:https://github.com/walkor/workerman-manual

Benchmarks

https://www.techempower.com/benchmarks/#section=data-r20&hw=ph&test=db&l=yyku7z-e7&a=2 image

Sponsors

opencollective.com/walkor

patreon.com/walkor

https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UQGGS9UB35WWG"> src="" target="_blank">http://donate.workerman.net/img/donate.png">

小讯
上一篇 2026-04-22 08:39
下一篇 2026-04-22 08:37

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/272706.html