Giter Club home page Giter Club logo

gatewayworker's Introduction

GatewayWorker

GatewayWorker基于Workerman开发的一个项目框架,用于快速开发长连接应用,例如app推送服务端、即时IM服务端、游戏服务端、物联网、智能家居等等。

GatewayWorker使用经典的Gateway和Worker进程模型。Gateway进程负责维持客户端连接,并转发客户端的数据给Worker进程处理;Worker进程负责处理实际的业务逻辑,并将结果推送给对应的客户端。Gateway服务和Worker服务可以分开部署在不同的服务器上,实现分布式集群。

GatewayWorker提供非常方便的API,可以全局广播数据、可以向某个群体广播数据、也可以向某个特定客户端推送数据。配合Workerman的定时器,也可以定时推送数据。

快速开始

开发者可以从一个简单的demo开始(demo中包含了GatewayWorker内核,以及start_gateway.php start_business.php等启动入口文件)
点击这里下载demo
demo说明见源码readme。

手册

http://www.workerman.net/gatewaydoc/

安装内核

只安装GatewayWorker内核文件(不包含start_gateway.php start_businessworker.php等启动入口文件)

composer require workerman/gateway-worker

使用GatewayWorker开发的项目

Live demo
Source code
workerman todpole

Live demo
Source code
workerman-chat

gatewayworker's People

Contributors

agui2200 avatar captainstdin avatar cxlblm avatar detain avatar dynatcs avatar hlxabcd avatar huangdijia avatar keppelcao avatar mouyong avatar ttlxihuan avatar twomiao avatar walkor avatar wenzhihong2003 avatar woann avatar xiasf avatar xuanyanwow avatar yurunsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gatewayworker's Issues

The log records some errors. How to handle them?

[ 2017-08-24T14:07:50+08:00 ][ error ] [2]file_get_contents(/var/www/game/vendor/workerman/workerman/../_var_www_game_worker_start.php.pid): failed to open stream: No such file or directory
[ 2017-08-24T14:07:57+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)
[ 2017-08-24T14:07:57+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)
[ 2017-08-24T14:07:57+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=11)
[ 2017-08-24T14:12:21+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=12)
[ 2017-08-24T14:12:21+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)
[ 2017-08-24T14:12:21+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)
[ 2017-08-24T14:22:38+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=12)
[ 2017-08-24T14:22:38+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)
[ 2017-08-24T14:22:38+08:00 ][ error ] [2]stream_select(): unable to select [4]: Interrupted system call (max_fd=9)

Set headers from Events.php

Hi Walkor

I am trying to set some custom headers when replying to client from inside Events.php run from businessWorker and can't figure out how to do it.

http server is started in the gateway.php with:
// gateway FwUpdHttpGateway process
$FwUpdHttpGateway = new Gateway("http://0.0.0.0:9090");

In Events.php
public static function onMessage($client_id, $message){
# Use Workerman\Protocols\Http::header to send header to clients.
# walkor/workerman#151
Http::header("Content-type: text/plain");
Http::header("Accept-Ranges: bytes");
Http::header("Content-Disposition: attachment; filename=test");

 		$fw_update_data = array(
		"version" => $fwupd_name,
		"firmware" => "?".$fwupd_name
	); 
		$fw_update_data = json_encode($fw_update_data);

		#Send data to client
     	 Gateway::sendToClient($_SERVER['GATEWAY_CLIENT_ID'], $fw_update_data);
}

Thankx

worker[ChatGateway:76703] exit with status 11

命令
git clone https://github.com/walkor/workerman-chat
cd workerman-chat/
composer install
php start.php start

** 版本**
Loading composer repositories with package information
Updating dependencies (including require-dev)

  • Installing workerman/workerman (v3.5.6)
    Loading from cache
  • Installing workerman/gateway-worker (v3.0.7)
    Loading from cache

出现下面的情况

Workerman[start.php] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.6 PHP version:7.0.27
------------------------ WORKERS -------------------------------
user worker listen processes status
xxxxx ChatBusinessWorker none 4 [OK]
xxxxx ChatGateway websocket://0.0.0.0:7272 4 [OK]
xxxxx Register text://0.0.0.0:1236 1 [OK]
xxxxx WebServer http://0.0.0.0:55151 2 [OK]

Press Ctrl+C to stop. Start success.
worker[ChatGateway:76065] exit with status 11
worker[ChatGateway:76064] exit with status 11
worker[ChatGateway:76063] exit with status 11
worker[ChatBusinessWorker:76062] exit with status 11
worker[ChatGateway:76066] exit with status 11
worker[ChatBusinessWorker:76061] exit with status 11
worker[ChatBusinessWorker:76060] exit with status 11
worker[ChatBusinessWorker:76059] exit with status 11
worker[ChatGateway:76072] exit with status 11
worker[ChatGateway:76071] exit with status 11
worker[ChatGateway:76073] exit with status 11
worker[ChatGateway:76075] exit with status 11
worker[ChatBusinessWorker:76074] exit with status 11
worker[ChatBusinessWorker:76076] exit with status 11
worker[ChatBusinessWorker:76078] exit with status 11
worker[ChatGateway:76079] exit with status 11
worker[ChatGateway:76080] exit with status 11
worker[ChatGateway:76082] exit with status 11
worker[ChatGateway:76081] exit with status 11
worker[ChatBusinessWorker:76083] exit with status 11
worker[ChatGateway:76086] exit with status 11
worker[ChatGateway:76087] exit with status 11
worker[ChatBusinessWorker:76085] exit with status 11
worker[ChatGateway:76088] exit with status 11
worker[ChatGateway:76089] exit with status 11
worker[ChatBusinessWorker:76084] exit with status 11
worker[ChatGateway:76092] exit with status 11
worker[ChatGateway:76091] exit with status 11
worker[ChatGateway:76094] exit with status 11
worker[ChatGateway:76095] exit with status 11
....

有没有一些测试的用例示范呢

业务逻辑可以用mockery之类模拟行为测试,那其他部分比如生命周期之类的,该怎么测试呢,能否提供一些建议或者示例呢

Socket Connection refused

I create inbound rule Custom TCP rule port 10001 in aws

I create socket server connection by Php GatewayWorker, the port is 10001.

telnet public ip result is No route to host, how to fix it?

[centos@ip-private ip ~]$ telnet [private ip] 10001
Trying [private ip]...
Connected to [private ip].
Escape character is '^]'.

[centos@ip-private ip ~]$ telnet [public ip] 10001
Trying [public ip]...
telnet: connect to address [public ip]: No route to host

[centos@ip-private ip ~]$ telnet [public ip] 80
Trying [public ip] ...
Connected to [public ip] .
Escape character is '^]'.

How to delete timer when code is not in Event.php

I already record Timer id in the memcached.
So I can get TimerID from memcached.
but How can I delete Timer when code is not in Event.php?
If I call Timer::del($timerIDFromMemcache), I think it's not working,
because the Timer doesn't initialize.

process_timeout

What is process_timeout? and why occurred?

2017-05-13 21:13:26 pid:23206 process_timeout:
#1 /home/app/vendor/workerman/workerman/Events/React/StreamSelectLoop.php(149): pcntl_signal_dispatch()
#2 /home/app/vendor/react/event-loop/src/StreamSelectLoop.php(225): Workerman\Events\React\StreamSelectLoop->streamSelect(Array, Array, 10000)
#3 /home/app/vendor/react/event-loop/src/StreamSelectLoop.php(205): React\EventLoop\StreamSelectLoop->waitForStreamActivity(10000)
#4 /home/app/vendor/workerman/workerman/Events/React/StreamSelectLoop.php(104): React\EventLoop\StreamSelectLoop->run()
#5 /home/app/vendor/workerman/workerman/Worker.php(1561): Workerman\Events\React\StreamSelectLoop->loop()
#6 /home/app/vendor/workerman/gateway-worker/src/BusinessWorker.php(183): Workerman\Worker->run()
#7 /home/app/vendor/workerman/workerman/Worker.php(959): GatewayWorker\BusinessWorker->run()
#8 /home/app/vendor/workerman/workerman/Worker.php(922): Workerman\Worker::forkOneWorker(Object(GatewayWorker\BusinessWorker))
#9 /home/app/vendor/workerman/workerman/Worker.php(430): Workerman\Worker::forkWorkers()
#10 /home/index.php(45): Workerman\Worker::runAll()
#11 {main}

Stop GatewayWorker server callback

Is there a server stop callback function in GatewayWorker?
because I want to record server shut down time,
when restart server, use the shut down time to recalculate the end time of my gameplay event.

Another question,If operating system or web server(nginx) restart,
how can I know the server previous shut down time?

Connection timed out

@walkor In distributed deployment I see the following error when I start new gateways in a new machine

stream_socket_client(): unable to connect to tcp://10.10.10.50:2320 (Connection timed out)
/vendor/workerman/gateway-worker/src/Lib/Gateway.php:363

10.10.10.50:2320 is a gateway in another machine and the load is very low

When I start new gateway in new machine , all business workers in all machines hangs for 10-15 seconds
I think we have stream_socket_client in business worker too and it's in synchronous mode

GatewayWorker status command is not working

$ php start.php start -d
$ php start.php status <<<< I can see the server status information

But second day, I can't see anything about status,why?
[centos@ip-172-31-13-63 GatewayWorker]$ php start.php status
PHP Warning: Module 'mcrypt' already loaded in Unknown on line 0
PHP Warning: Module 'msgpack' already loaded in Unknown on line 0
Workerman[start.php] status

分布式部署多个gateway时,不同的client如何通信?

client1需要和client2通信,client1连接到gateway1, gateway1把client1的信息转发给businessworker1, client2连接到的gateway2, 此时,businessworker1怎么把消息转发到gateway2上的?因为gateway1并没有client1的连接。谢谢~

can't trigger Timer function

$task = new Worker();
$task->name = 'YourAppTestTimer';
$task->count = 1;
$task->onWorkerStart = function($task)
{
$time_interval = 2.5;
Timer::add($time_interval, "timerProcess",array(),true);
};

function timerProcess(){
$t = ORM::for_table("test")->find_one(1);
$t->end_time = Carbon::now()->toDateTimeString();
$t->save();

echo "task run\n";

}
Worker::runAll();

use "php start.php start" it will execute timerProcess every 2.5 seconds.
But use "php start.php start -d" , it won't exceute timerProcess function.why?

sentToUid和sendToClient推送失败

冒昧的请教下:在使用GatewayWorker的时候(websocket),在events.php文件onMessage函数里面使用sendToAll能推送给客户端成功接收,但是使用sendToUid和sendToClient给指定客户端推送消息,可能是什么原因呢?

gatewaywork集群下有多台gateway服务器,客户端(PC、APP)应该连接哪一个gateway?

gatewaywork集群下有多台gateway服务器,客户端(PC、APP)应该连接哪一个gateway?
比如:
1,192.168.1.1机器的start_gateway.php配置$gateway = new Gateway("Websocket://192.168.1.1:7272");
2,192.168.1.2机器的start_gateway.php配置$gateway = new Gateway("Websocket://192.168.1.2:7272");
这时候客户端应该如何做,还是随机的连接这两个gateway任意一个?

Getting more done in GitHub with ZenHub

Hola! @dandanlee has created a ZenHub account for the walkor organization. ZenHub is the only project management tool integrated natively in GitHub – created specifically for fast-moving, software-driven teams.


How do I use ZenHub?

To get set up with ZenHub, all you have to do is download the browser extension and log in with your GitHub account. Once you do, you’ll get access to ZenHub’s complete feature-set immediately.

What can ZenHub do?

ZenHub adds a series of enhancements directly inside the GitHub UI:

  • Real-time, customizable task boards for GitHub issues;
  • Multi-Repository burndown charts, estimates, and velocity tracking based on GitHub Milestones;
  • Personal to-do lists and task prioritization;
  • Time-saving shortcuts – like a quick repo switcher, a “Move issue” button, and much more.

Add ZenHub to GitHub

Still curious? See more ZenHub features or read user reviews. This issue was written by your friendly ZenHub bot, posted by request from @dandanlee.

ZenHub Board

react async mysql in gateway

Hi, worker,
according to the example in workman, I used it in gateway, but it doesn't work, the client could not receive the message!

$asyncMysql->query('select * from user', function ($command, $mysql) {
            if ($command->hasError()) {
                $error = $command->getError();
            } else {
                $results = $command->resultRows;
                $fields  = $command->resultFields;
                \GatewayWorker\Lib\Gateway::sendToCurrentClient(json_encode($results));
            }
        });

I also use sendToUid replace to sendToCurrentClient ..... same problem
Do I need use $connections as workerman example? If so, how can i get $connect in gateway

React/stomp (要求Workerman版本>=3.3.6)

Hello Walkor,

Very nice work you did with the Worker project.
While looking at documentation for GatewayWorker I found out on React/stomp page that
Note: This item has expired and cannot be used. Do not install

Is this true? Is there a versioning mismatch ?
Can it now be used?

Thank you.

Limits

@walkor if we have many powerful machines
How many gateway and business worker can exist in workerman system ??

If we choose libevent or event as main event loop

Questions

1- What is the memory showing in the php index.php status , current using memory? Peak memory?

2- How Gateway (G) , Business worker (B) and Register (R) is connected together?
If we have 1 R , 10 G and 20 B , how are connected? and how many connection have R , B and G?

3- If I have a GatewayClient in my BusinessWorker and I use sendToClient OR sendToUid , What happens exactly? If I have 10 Gateway , I think for both command each 10 gateway receive a message

$_SESSION未及时更新

是在直播间进出房间的场景,用户进入直播间A,我在当前用户的session里记录A $_SESSION['rooms']['A']=1,用户退出A时,我把A从session里unset掉 unset($_SESSION['rooms']['A'])。现在是网页里用过websocket建立连接,用户关闭页面时用户退出A,随即网页关闭,websocket连接断开了。
问题:我会在后端socket断开连接的回调里检测用户是否还有未关闭的房间,有的话会把用户退出房间,可能是 退出房间和socket连接间隔太短了,退出房间时session更新了,但回调时这时session里的A并没清除,从而导致用户会再退出A一次。
Workerman version:3.3.2
GatewayWorker master分支
PHP version:5.6.2
CentOS 7

65280错误代码日志

1.用status命令查看,有很多65280的错误。这种错误日志在什么地方? 还是需要自己代码里面做处理?
2.默认的项目里面也没有生成workerman.log日志,是需要设置什么?
麻烦帮忙答疑,谢谢!

createGlobalClientId failed

看到最近的更新修复了上一个版本中createGlobalClientId failed这个问题,commit message里没有提到,觉得还是提示下同样遇到这个问题的童鞋,以便交流:
2.0.5之前的createGlobalClientId()依赖Redis/Memcached生成client_id(global_client_id),createGlobalClientId()函数里没有处理Redis/Memcached异常,而直接将返回值作为key保存客户端信息。可能导致后续Redis恢复,部分连接无法正常更新Redis中数据,需要重启Workman。

2.0.5里generateConnectionId()使用变量_connectionIdRecorder生成client_id,不依赖Redis,建议大家使用这个修改。

Worker names

Hi
What happens if I do this , I deployment distribution , I set different name for gateways , for example in server 1 I set the name of gateway as GS1 and in server 1 I set GS2 , is there any issue if I do this?

Log file doesn't often show new log

I open three GatewayWorker Servers in an operating system.
I have the same code in three different folder ,and the folder structure is below:

Peter or Leon or Test folder
-GatewayWorker
--Applications
--- YourApp
---- Config
----- Config.php (add by myslef)
------ Db.php
------ Store.php
...(Skip)

this is Test's Config.php code blow:
$port = 8282;
$logFileNamePostfix = "";

this is Peter's Config.php code blow:
$port = 8283;
$logFileNamePostfix = "Peter";

this is Leon's Config.php code blow:
$port = 8284;
$logFileNamePostfix = "Leon";

the $port will apply for "$gateway = new Gateway("Text://0.0.0.0:".$port);"
the $logFileNamePostfix will apply for
"Worker::$stdoutFile = '/var/log/TTT_SocketServer_'.$logFileNamePostfix.Carbon::now()->toDateString().'.log';" in start_bussinessworker.php

So I execute "php start.php start -d" in three different folder, I get three server using different port.
and output server log into three different file.

I found that three Log file doesn't often show new log,sometimes it show log,but sometimes it dosen't.
why?

Client Id

I want to know how clientId is generated?

Is it unique in multiple server ( Distributed Deployment )? that means If we have 3 server and I have clientId A in server 1 , is it possible some one or me have A as client id in server 2 or 3 ?

Is it unique in time , that means If my clientId is B and I close connection is it possible later some one or me have B as client id?

System Hangs

I have 8 machine and run in each machine 10 gateway and 10 business worker

I have heavy loads , and have a bad problem

In each machine CPU usage of all gateways is same , when in a single machine , CPU usage of gateways reaches 100% Whole system (All users in all servers) hangs and not works , even when other machines CPU usage of gateways and business worker is 10-30%

Currently I'm using stream_select , I think when a business worker send message to gatway with 100% CPU usage , The PHP locked on the line and with timeout skip from the line ? Right? or not?

Where is problem? How to solve?

Event回调,onWebSocketConnect, onConnect ,onMessage在并发场景中的bug

Event回调,onWebSocketConnect, onConnect ,onMessage在并发场景中的bug

bug场景:
onWebSocketConnect 或者是 onConnect 对首次接入 做拒绝处理,或授权验证 (处理结果如:
Gateway::closeCurrentClient ,或者直接exit)。

但是在压测高并发场景下 ,仍然会穿透给 onMessage ,也就是说, 如果想攻击你 ,拿着消息体 连接 gateway 即可。(如 拿着发送消息的json结构,不断攻击连接端口,则会造成穿透至 onMessage )

Gateway不能和Register采用相同的配置格式

例如

`
$coket = ‘text://0.0.0.0:3238’;
new Register($coket);

$gateway = new Gateway("ws://0.0.0.0:8070");
$gateway->registerAddress = $socket;
`
报错信息

Warning: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known in

how to onmessage for laravel framework

@walkor 我想应用您写的GatewayWorker,我在laravel console 分别创建了StartRegister.php、StartBusinessworker.php、StartGateway.php 我在浏览器console请求websocket是成功的,请问在laravel 框架里我改怎么接受 websocket 数据呢?

Integration with swoole

Hello

Swoole is an Event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP.

Here is Github repository https://github.com/swoole/swoole-src

There is some advantage over workerman , So I want to replace GatewayWorker with swoole but I want to connect swoole to register and business worker , How is possible?

I want to keep all GatewayWorker features like Distributed deployment

Best Regards

packet size limit

I found if the data size is large, sendToGroup() will split the string then send next time.
How to know and set packet size limit?

php7.3.2下,使用thinkphp5.1的数据库连接,导致worker进程退出

测试机:mac10.14.1
php环境:7.3.2
框架版本:thinkphp5.1
gatewayworker版本:"workerman/gateway-worker": "^3.0"
错误:使用th5的orm或连接导致worker进程退出;worker[MedicalBoxWorker:920] exit with status 11
补充:
(1)此错误Exception无法捕获;
(2)在php7.1环境没有问题;
(3)Worker::runAll()启动前可用使用DB,Events中的回调(onWorkerStart,onConnect,onMessage)无法使用

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.