Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9f2264

Browse files
committed
使用workerman来代替提供websocket服务,去掉不稳定的laravel-echo-server服务
1 parent 62c8d0e commit c9f2264

File tree

142 files changed

+43766
-3364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+43766
-3364
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* This file is part of workerman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<walkor@workerman.net>
10+
* @copyright walkor<walkor@workerman.net>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
/**
16+
* 用于检测业务代码死循环或者长时间阻塞等问题
17+
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
18+
* 然后观察一段时间workerman.log看是否有process_timeout异常
19+
*/
20+
//declare(ticks=1);
21+
22+
use \GatewayWorker\Lib\Gateway;
23+
24+
/**
25+
* 主逻辑
26+
* 主要是处理 onConnect onMessage onClose 三个方法
27+
* onConnect 和 onClose 如果不需要可以不用实现并删除
28+
*/
29+
class Events
30+
{
31+
/**
32+
* 当客户端连接时触发
33+
* 如果业务不需此回调可以删除onConnect
34+
*
35+
* @param int $client_id 连接id
36+
*/
37+
public static function onConnect($client_id)
38+
{
39+
$data = [
40+
'type' => 'init',
41+
'client_id' => $client_id
42+
];
43+
Gateway::sendToClient(json_decode($data));
44+
}
45+
46+
/**
47+
* 当客户端发来消息时触发
48+
* @param int $client_id 连接id
49+
* @param mixed $message 具体消息
50+
*/
51+
public static function onMessage($client_id, $message)
52+
{
53+
// 向所有人发送
54+
$res = json_decode($message, true);
55+
if ($res['type'] === 'ping'){
56+
// 心跳包 前端50秒发送一次
57+
58+
}
59+
// Gateway::sendToAll("$client_id said $message\r\n");
60+
}
61+
62+
/**
63+
* 当用户断开连接时触发
64+
* @param int $client_id 连接id
65+
*/
66+
public static function onClose($client_id)
67+
{
68+
// 向所有人发送
69+
// GateWay::sendToAll("$client_id logout\r\n");
70+
}
71+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* This file is part of workerman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<walkor@workerman.net>
10+
* @copyright walkor<walkor@workerman.net>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
use \Workerman\Worker;
15+
use \Workerman\WebServer;
16+
use \GatewayWorker\Gateway;
17+
use \GatewayWorker\BusinessWorker;
18+
use \Workerman\Autoloader;
19+
20+
// 自动加载类
21+
require_once __DIR__ . '/../../vendor/autoload.php';
22+
23+
// bussinessWorker 进程
24+
$worker = new BusinessWorker();
25+
// worker名称
26+
$worker->name = 'YourAppBusinessWorker';
27+
// bussinessWorker进程数量
28+
$worker->count = 4;
29+
// 服务注册地址
30+
$worker->registerAddress = '127.0.0.1:1680';
31+
32+
// 如果不是在根目录启动,则运行runAll方法
33+
if(!defined('GLOBAL_START'))
34+
{
35+
Worker::runAll();
36+
}
37+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* This file is part of workerman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<walkor@workerman.net>
10+
* @copyright walkor<walkor@workerman.net>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
use \Workerman\Worker;
15+
use \Workerman\WebServer;
16+
use \GatewayWorker\Gateway;
17+
use \GatewayWorker\BusinessWorker;
18+
use \Workerman\Autoloader;
19+
20+
// 自动加载类
21+
require_once __DIR__ . '/../../vendor/autoload.php';
22+
23+
// gateway 进程,这里使用Text协议,可以用telnet测试
24+
$gateway = new Gateway("websocket://0.0.0.0:1800");
25+
// gateway名称,status方便查看
26+
$gateway->name = 'YourAppGateway';
27+
// gateway进程数
28+
$gateway->count = 4;
29+
// 本机ip,分布式部署时使用内网ip
30+
$gateway->lanIp = '127.0.0.1';
31+
// 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
32+
// 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
33+
$gateway->startPort = 2900;
34+
// 服务注册地址
35+
$gateway->registerAddress = '127.0.0.1:1680';
36+
37+
// 心跳间隔
38+
//$gateway->pingInterval = 10;
39+
// 心跳数据
40+
//$gateway->pingData = '{"type":"ping"}';
41+
42+
/*
43+
// 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
44+
$gateway->onConnect = function($connection)
45+
{
46+
$connection->onWebSocketConnect = function($connection , $http_header)
47+
{
48+
// 可以在这里判断连接来源是否合法,不合法就关掉连接
49+
// $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
50+
if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
51+
{
52+
$connection->close();
53+
}
54+
// onWebSocketConnect 里面$_GET $_SERVER是可用的
55+
// var_dump($_GET, $_SERVER);
56+
};
57+
};
58+
*/
59+
60+
// 如果不是在根目录启动,则运行runAll方法
61+
if(!defined('GLOBAL_START'))
62+
{
63+
Worker::runAll();
64+
}
65+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* This file is part of workerman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<walkor@workerman.net>
10+
* @copyright walkor<walkor@workerman.net>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
use \Workerman\Worker;
15+
use \GatewayWorker\Register;
16+
17+
// 自动加载类
18+
require_once __DIR__ . '/../../vendor/autoload.php';
19+
20+
// register 必须是text协议
21+
$register = new Register('text://0.0.0.0:1680');
22+
23+
// 如果不是在根目录启动,则运行runAll方法
24+
if(!defined('GLOBAL_START'))
25+
{
26+
Worker::runAll();
27+
}
28+

‎GatewayWorker_linux/MIT-LICENSE.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2009-2015 walkor<walkor@workerman.net> and contributors (see https://github.com/walkor/workerman/contributors)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎GatewayWorker_linux/README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
GatewayWorker windows 版本
2+
=================
3+
4+
GatewayWorker基于[Workerman](https://github.com/walkor/Workerman)开发的一个项目框架,用于快速开发长连接应用,例如app推送服务端、即时IM服务端、游戏服务端、物联网、智能家居等等。
5+
6+
GatewayWorker使用经典的Gateway和Worker进程模型。Gateway进程负责维持客户端连接,并转发客户端的数据给Worker进程处理;Worker进程负责处理实际的业务逻辑,并将结果推送给对应的客户端。Gateway服务和Worker服务可以分开部署在不同的服务器上,实现分布式集群。
7+
8+
GatewayWorker提供非常方便的API,可以全局广播数据、可以向某个群体广播数据、也可以向某个特定客户端推送数据。配合Workerman的定时器,也可以定时推送数据。
9+
10+
GatewayWorker Linux 版本
11+
======================
12+
Linux 版本GatewayWorker 在这里 https://github.com/walkor/GatewayWorker
13+
14+
启动
15+
=======
16+
双击start_for_win.bat
17+
18+
Applications\YourApp测试方法
19+
======
20+
使用telnet命令测试(不要使用windows自带的telnet)
21+
```shell
22+
telnet 127.0.0.1 8282
23+
Trying 127.0.0.1...
24+
Connected to 127.0.0.1.
25+
Escape character is '^]'.
26+
Hello 3
27+
3 login
28+
haha
29+
3 said haha
30+
```
31+
32+
手册
33+
=======
34+
http://www.workerman.net/gatewaydoc/
35+
36+
使用GatewayWorker-for-win开发的项目
37+
=======
38+
## [tadpole](http://kedou.workerman.net/)
39+
[Live demo](http://kedou.workerman.net/)
40+
[Source code](https://github.com/walkor/workerman)
41+
![workerman-todpole](http://www.workerman.net/img/workerman-todpole.png)
42+
43+
## [chat room](http://chat.workerman.net/)
44+
[Live demo](http://chat.workerman.net/)
45+
[Source code](https://github.com/walkor/workerman-chat)
46+
![workerman-chat](http://www.workerman.net/img/workerman-chat.png)

‎GatewayWorker_linux/composer.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name" : "workerman/gateway-worker-demo",
3+
"keywords": ["distributed","communication"],
4+
"homepage": "http://www.workerman.net",
5+
"license" : "MIT",
6+
"require": {
7+
"workerman/gateway-worker" : ">=3.0.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"" : "./"
12+
}
13+
}
14+
}

‎GatewayWorker_linux/start.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* run with command
4+
* php start.php start
5+
*/
6+
7+
ini_set('display_errors', 'on');
8+
use Workerman\Worker;
9+
10+
if(strpos(strtolower(PHP_OS), 'win') === 0)
11+
{
12+
exit("start.php not support windows, please use start_for_win.bat\n");
13+
}
14+
15+
// 检查扩展
16+
if(!extension_loaded('pcntl'))
17+
{
18+
exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
19+
}
20+
21+
if(!extension_loaded('posix'))
22+
{
23+
exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
24+
}
25+
26+
// 标记是全局启动
27+
define('GLOBAL_START', 1);
28+
29+
require_once __DIR__ . '/vendor/autoload.php';
30+
31+
// 加载所有Applications/*/start.php,以便启动所有服务
32+
foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file)
33+
{
34+
require_once $start_file;
35+
}
36+
// 运行所有服务
37+
Worker::runAll();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
php Applications\YourApp\start_register.php Applications\YourApp\start_gateway.php Applications\YourApp\start_businessworker.php
2+
pause
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer/autoload_real.php';
6+
7+
return ComposerAutoloaderInit8f3411af7a7a58e545cca1dc386d93c8::getLoader();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /