Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
da3895f
fix test namespace
albertcht Apr 17, 2018
9a1db6b
try to support minimum socket.io protocol
albertcht Apr 18, 2018
450c12f
set alias for swoole.server
albertcht Apr 18, 2018
cb63a43
Revert "set alias for swoole.server"
albertcht Apr 18, 2018
bfc82a3
add support for websocket transport
albertcht Apr 18, 2018
6d9868a
improve payload variable
albertcht Apr 18, 2018
5f28ffe
add support for plain text message
albertcht Apr 18, 2018
815571d
add tests for socket.io
albertcht Apr 18, 2018
ed9c1f9
remove useless namespace
albertcht Apr 18, 2018
38430c9
add namespace
albertcht Apr 18, 2018
1831fc6
add routes for socket.io
albertcht Apr 19, 2018
541023a
try to fix boot routes for laravel
albertcht Apr 19, 2018
7f31b49
fix boot routes for lumen
albertcht Apr 19, 2018
19d0587
add route facade for laravel
albertcht Apr 19, 2018
e0007b8
fix typo
albertcht Apr 19, 2018
454f62b
apply ping interval in config
albertcht Apr 19, 2018
c5e8ae5
move websocket register to service provider
albertcht Apr 19, 2018
b6d8051
add register methods
albertcht Apr 19, 2018
cacedfb
move WebsocketHandler to sockt.io
albertcht Apr 19, 2018
5c930c5
add 'Transport unknown' response
albertcht Apr 19, 2018
d986e18
improve getPayload function
albertcht Apr 19, 2018
ed9ce13
improve getFds function
albertcht Apr 19, 2018
f8e0a6c
support callbacks for websocket
albertcht Apr 19, 2018
e0d3457
fix packet parser for null data
albertcht Apr 19, 2018
c8aed01
remove websocket register from service provider
albertcht Apr 19, 2018
f913422
fix fire event
albertcht Apr 19, 2018
23f8a1d
add data param for websocket on
albertcht Apr 19, 2018
3d148e9
rewrite websocket callback process
albertcht Apr 20, 2018
c071131
try to fix bound method call under laravel 5.4
albertcht Apr 20, 2018
cf523b4
remove redundant code
albertcht Apr 20, 2018
58606aa
remove redundant test
albertcht Apr 20, 2018
0009452
remove useless test code
albertcht Apr 20, 2018
c1e5c2c
add callback test code
albertcht Apr 20, 2018
7a28ca2
fix syntax error and bug
albertcht Apr 20, 2018
17ca20e
add normalizePushData function
albertcht Apr 20, 2018
065269c
delete redundant code
albertcht Apr 20, 2018
7655759
Revert "delete redundant code"
albertcht Apr 20, 2018
28599f6
fix websocket reset
albertcht Apr 20, 2018
29f7218
remove useless namespace
albertcht Apr 20, 2018
3bded97
fix onShutdown event fire
albertcht Apr 20, 2018
0061d2c
modify default reject response for socket.io
albertcht Apr 20, 2018
779a453
improve reset websocket
albertcht Apr 20, 2018
67a99d6
trigger connect and disconnect websocket event
albertcht Apr 20, 2018
436472c
leave all rooms before trigger disconnect
albertcht Apr 20, 2018
d77a6c7
return self after calling reset
albertcht Apr 20, 2018
6bb9631
emit received data in default websocket example routes
albertcht Apr 20, 2018
ba32768
clear user-defined instances on every request
albertcht Apr 20, 2018
8a30d76
uncomment instances config
albertcht Apr 20, 2018
8820260
add auto discovery info
albertcht Apr 21, 2018
9b3bb43
support laravel's exception handler on request
albertcht Apr 21, 2018
7d62624
improve exception response
albertcht Apr 21, 2018
89cb593
collect bindings to bindToLaravelApp function
albertcht Apr 21, 2018
a94fc62
clear customized facades and instances on every request
albertcht Apr 21, 2018
d2324b9
change publish tag name to 'laravel-swoole'
albertcht Apr 21, 2018
68d7313
fix config init value
albertcht Apr 22, 2018
d42580d
add sandbox_mode
albertcht Apr 22, 2018
7d243da
require deep-copy package
albertcht Apr 22, 2018
3690bc9
add lumen router to sandbox's whitelist
albertcht Apr 22, 2018
54ce594
substitude deepCopy with simple clone for better performance
albertcht Apr 22, 2018
f610f02
clearResolvedInstances while being cloned
albertcht Apr 22, 2018
3c19552
improve resetApplication after clone
albertcht Apr 22, 2018
ab40bc8
bind illuminate request to laravel instance on request
albertcht Apr 23, 2018
d07dee3
try to reconstruct sandbox
albertcht Apr 23, 2018
a944ddf
fix a null pointer bug
albertcht Apr 23, 2018
8cc82c0
fix request
albertcht Apr 23, 2018
c4df349
clean session and cookie after terminating laravel
albertcht Apr 24, 2018
7b6a24d
pre resolve some instances before request
albertcht Apr 24, 2018
9ad0c93
load application on bootstrap
albertcht Apr 24, 2018
48661c8
check before resolving instance
albertcht Apr 24, 2018
0829213
use offsetExists instead of has
albertcht Apr 24, 2018
954c2c3
list more pre-resoved values
albertcht Apr 24, 2018
78c1c49
modify default recommended providers in config
albertcht Apr 24, 2018
2b29976
fix kernel's app pointer while cloning
albertcht Apr 25, 2018
7600b3d
fix router's container binding
albertcht Apr 25, 2018
2585e2c
delete test code
albertcht Apr 25, 2018
b70fcd2
add enabled property to sandbox
albertcht Apr 25, 2018
d0c9eb1
add ob to response
albertcht Apr 25, 2018
6ba1e66
exclude StreamedResponse and BinaryFileResponse in ob process
albertcht Apr 25, 2018
ade1376
add ob_output config
albertcht Apr 25, 2018
2f6f642
make sandbox static
albertcht Apr 25, 2018
1311d08
wrap more code with try in onRequest function
albertcht Apr 25, 2018
a831027
support lumen's router app rebinding
albertcht Apr 25, 2018
fc3670d
clean sandbox test
albertcht Apr 25, 2018
d4cc1a2
fix setInstance in sandbox
albertcht Apr 25, 2018
474fa0e
fix routes' container binding
albertcht Apr 25, 2018
4925722
abandon rebindRoutesContainer function
albertcht Apr 26, 2018
3fdc54c
fix websocket push while no destinations are assigned
albertcht Apr 26, 2018
fa5409f
improve sandbox class
albertcht Apr 26, 2018
619cc52
turn on facades while bootstraping lumen if not enabled
albertcht Apr 26, 2018
f3ed8e5
delete useless comment
albertcht Apr 26, 2018
3d435ac
set default buffer_output_size to 10M
albertcht Apr 26, 2018
94185c0
add some comment for config
albertcht Apr 26, 2018
5d261b8
extract table feature to trait
albertcht Apr 27, 2018
1492138
add property and function explaination
albertcht Apr 27, 2018
ecd01b7
add redis driver for room
albertcht Apr 28, 2018
a7eb672
remove illuminate/redis dependency. use predis client directly
albertcht Apr 28, 2018
5155142
fix redis room getValue
albertcht Apr 28, 2018
dc20b16
fix redis room
albertcht Apr 28, 2018
e6481db
make redis room prefix customizable
albertcht Apr 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"illuminate/console": "~5.1",
"illuminate/contracts": "~5.1",
"illuminate/http": "~5.1",
"illuminate/support": "~5.1"
"illuminate/support": "~5.1",
"predis/predis": "^1.1"
},
"require-dev": {
"laravel/lumen-framework": "~5.1",
Expand All @@ -36,5 +37,18 @@
"SwooleTW\\Http\\Tests\\": "tests",
"SwooleTW\\Http\\Tests\\Fixtures\\Laravel\\App\\": "tests/fixtures/laravel/app"
}
},
"extra": {
"laravel": {
"providers": [
"SwooleTW\\Http\\LaravelServiceProvider"
],
"aliases": {
"Server": "SwooleTW\\Http\\Server\\Facades\\Server",
"Table": "SwooleTW\\Http\\Server\\Facades\\Table",
"Room": "SwooleTW\\Http\\Websocket\\Facades\\Room",
"Websocket": "SwooleTW\\Http\\Websocket\\Facades\\Websocket"
}
}
}
}
45 changes: 42 additions & 3 deletions config/swoole_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
| HTTP server configurations.
|--------------------------------------------------------------------------
|
| @see https://wiki.swoole.com/wiki/page/274.html
| @see https://www.swoole.co.uk/docs/modules/swoole-server/configuration
|
*/
'server' => [
Expand All @@ -18,21 +18,60 @@
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', 4)
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', 4),
// The data to send can't be larger than buffer_output_size.
'buffer_output_size' => 10 * 1024 * 1024
],
],

/*
|--------------------------------------------------------------------------
| Enable to turn on websocket server.
|--------------------------------------------------------------------------
*/
'websocket' => [
'enabled' => env('SWOOLE_HTTP_WEBSOCKET', false),
],

/*
|--------------------------------------------------------------------------
| Laravel app will be cloned on every requset.
|--------------------------------------------------------------------------
*/
'sandbox_mode' => env('SWOOLE_SANDBOX_MODE', false),

/*
|--------------------------------------------------------------------------
| Console output will be transfered to response content if enabled.
|--------------------------------------------------------------------------
*/
'ob_output' => env('SWOOLE_OB_OUTPUT', true),

/*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
// App\Providers\AuthServiceProvider::class,
// Illuminate\Auth\AuthServiceProvider::class,
],

/*
|--------------------------------------------------------------------------
| Resolved facades here will be cleared on every request.
|--------------------------------------------------------------------------
*/
'facades' => [
'auth', 'auth.driver', 'auth.password', 'request'
],

/*
|--------------------------------------------------------------------------
| Instances here will be cleared on every request.
|--------------------------------------------------------------------------
*/
'instances' => [
//
],

/*
Expand Down
41 changes: 34 additions & 7 deletions config/swoole_websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
| Replace this handler before you start it
|--------------------------------------------------------------------------
*/
'handler' => SwooleTW\Http\Websocket\WebsocketHandler::class,
'handler' => SwooleTW\Http\Websocket\SocketIO\WebsocketHandler::class,

/*
|--------------------------------------------------------------------------
| Websocket handlers mapping for onMessage callback
| Websocket route file path
|--------------------------------------------------------------------------
*/
'handlers' => [
// 'event_name' => 'App\Handlers\ExampleHandler@function',
],
'route_file' => base_path('routes/websocket.php'),

/*
|--------------------------------------------------------------------------
Expand All @@ -27,11 +25,25 @@

/*
|--------------------------------------------------------------------------
| Default message formatter
| Default frame parser
| Replace it if you want to customize your websocket payload
|--------------------------------------------------------------------------
*/
'formatter' => SwooleTW\Http\Websocket\Formatters\DefaultFormatter::class,
'parser' => SwooleTW\Http\Websocket\SocketIO\SocketIOParser::class,

/*
|--------------------------------------------------------------------------
| Heartbeat interval (ms)
|--------------------------------------------------------------------------
*/
'ping_interval' => 25000,

/*
|--------------------------------------------------------------------------
| Heartbeat interval timeout (ms)
|--------------------------------------------------------------------------
*/
'ping_timeout' => 60000,

/*
|--------------------------------------------------------------------------
Expand All @@ -40,6 +52,7 @@
*/
'drivers' => [
'table' => SwooleTW\Http\Websocket\Rooms\TableRoom::class,
'redis' => SwooleTW\Http\Websocket\Rooms\RedisRoom::class,
],

/*
Expand All @@ -54,6 +67,20 @@
'room_size' => 2048,
'client_rows' => 8192,
'client_size' => 2048
],

'redis' => [
'server' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'persistent' => true,
],
'options' => [
//
],
'prefix' => 'swoole:',
]
],
];
17 changes: 17 additions & 0 deletions routes/laravel_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register socket.io routes for your application.
|
*/

Route::group(['namespace' => 'SwooleTW\Http\Controllers'], function () {
Route::get('socket.io', 'SocketIOController@upgrade');
Route::post('socket.io', 'SocketIOController@reject');
});
20 changes: 20 additions & 0 deletions routes/lumen_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
|--------------------------------------------------------------------------
| Socket.io Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app->get('socket.io', [
'as' => 'io.get', 'uses' => 'SocketIOController@upgrade'
]);

$app->post('socket.io', [
'as' => 'io.post', 'uses' => 'SocketIOController@reject'
]);
18 changes: 18 additions & 0 deletions routes/websocket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use SwooleTW\Http\Websocket\Facades\Websocket;

/*
|--------------------------------------------------------------------------
| Websocket Routes
|--------------------------------------------------------------------------
|
| Here is where you can register websocket events for your application.
|
*/

Websocket::on('example', function ($websocket, $data) {
$websocket->emit('message', $data);
});

// Websocket::on('test', 'ExampleController@method');
41 changes: 41 additions & 0 deletions src/Controllers/SocketIOController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace SwooleTW\Http\Controllers;

use Illuminate\Http\Request;

class SocketIOController
{
protected $transports = ['polling', 'websocket'];

public function upgrade(Request $request)
{
if (! in_array($request->input('transport'), $this->transports)) {
return response()->json([
'code' => 0,
'message' => 'Transport unknown'
], 400);
}

if ($request->has('sid')) {
return '1:6';
}

$payload = json_encode([
'sid' => base64_encode(uniqid()),
'upgrades' => ['websocket'],
'pingInterval' => config('swoole_websocket.ping_interval'),
'pingTimeout' => config('swoole_websocket.ping_timeout')
]);

return '97:0' . $payload . '2:40';
}

public function reject(Request $request)
{
return response()->json([
'code' => 3,
'message' => 'Bad request'
], 400);
}
}
13 changes: 11 additions & 2 deletions src/HttpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function register()
*/
abstract protected function registerManager();

/**
* Boot routes.
*
* @return void
*/
abstract protected function bootRoutes();

/**
* Boot the service provider.
*
Expand All @@ -42,8 +49,10 @@ public function boot()
{
$this->publishes([
__DIR__ . '/../config/swoole_http.php' => base_path('config/swoole_http.php'),
__DIR__ . '/../config/swoole_websocket.php' => base_path('config/swoole_websocket.php')
], 'config');
__DIR__ . '/../config/swoole_websocket.php' => base_path('config/swoole_websocket.php'),
__DIR__ . '/../routes/websocket.php' => base_path('routes/websocket.php')
], 'laravel-swoole');
$this->bootRoutes();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ protected function registerManager()
return new Manager($app, 'laravel');
});
}

/**
* Boot routes.
*
* @return void
*/
protected function bootRoutes()
{
require __DIR__.'/../routes/laravel_routes.php';
}
}
20 changes: 20 additions & 0 deletions src/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,24 @@ protected function registerManager()
return new Manager($app, 'lumen');
});
}

/**
* Boot routes.
*
* @return void
*/
protected function bootRoutes()
{
$app = $this->app;

if (property_exists($app, 'router')) {
$app->router->group(['namespace' => 'SwooleTW\Http\Controllers'], function ($app) {
require __DIR__ . '/../routes/lumen_routes.php';
});
} else {
$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
require __DIR__ . '/../routes/lumen_routes.php';
});
}
}
}
Loading