作者 lyh

gx

  1 +<?php
  2 +
  3 +namespace App\Broadcasting;
  4 +
  5 +use App\Models\User\User;
  6 +
  7 +class WebSocketChannel
  8 +{
  9 + /**
  10 + * Create a new channel instance.
  11 + *
  12 + * @return void
  13 + */
  14 + public function __construct()
  15 + {
  16 + //
  17 + }
  18 +
  19 + /**
  20 + * Authenticate the user's access to the channel.
  21 + *
  22 + * @param \App\Models\User\User $user
  23 + * @return array|bool
  24 + */
  25 + public function join(User $user)
  26 + {
  27 + //
  28 + }
  29 +}
@@ -3,13 +3,16 @@ @@ -3,13 +3,16 @@
3 namespace App\Events; 3 namespace App\Events;
4 4
5 use Illuminate\Broadcasting\Channel; 5 use Illuminate\Broadcasting\Channel;
  6 +use Illuminate\Broadcasting\InteractsWithSockets;
  7 +use Illuminate\Broadcasting\PresenceChannel;
  8 +use Illuminate\Broadcasting\PrivateChannel;
6 use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 9 use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
7 use Illuminate\Foundation\Events\Dispatchable; 10 use Illuminate\Foundation\Events\Dispatchable;
8 use Illuminate\Queue\SerializesModels; 11 use Illuminate\Queue\SerializesModels;
9 12
10 -class WebSocketMessageSent implements ShouldBroadcast 13 +class WebSocketMessage implements ShouldBroadcast
11 { 14 {
12 - use Dispatchable, SerializesModels; 15 + use Dispatchable, InteractsWithSockets, SerializesModels;
13 16
14 public $message; 17 public $message;
15 18
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 namespace App\Http\Controllers\Bside; 10 namespace App\Http\Controllers\Bside;
11 11
12 use App\Enums\Common\Code; 12 use App\Enums\Common\Code;
  13 +use App\Events\WebSocketMessage;
13 use App\Events\WebSocketMessageSent; 14 use App\Events\WebSocketMessageSent;
14 use App\Helper\Common; 15 use App\Helper\Common;
15 use App\Helper\Translate; 16 use App\Helper\Translate;
@@ -146,7 +147,7 @@ class LoginController extends BaseController @@ -146,7 +147,7 @@ class LoginController extends BaseController
146 // $message = file_get_contents("php://input"); 147 // $message = file_get_contents("php://input");
147 // $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR); 148 // $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
148 // @file_put_contents(storage_path('logs/lyh_error.log'), $message . PHP_EOL, FILE_APPEND); 149 // @file_put_contents(storage_path('logs/lyh_error.log'), $message . PHP_EOL, FILE_APPEND);
149 - event(new WebSocketMessageSent('New message from server')); 150 + event(new WebSocketMessage('Hello, WebSocket!'));
150 $this->response('success'); 151 $this->response('success');
151 } 152 }
152 153
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 "keywords": ["framework", "laravel"], 5 "keywords": ["framework", "laravel"],
6 "license": "MIT", 6 "license": "MIT",
7 "require": { 7 "require": {
8 - "php": "^7.4|^8.1", 8 + i
9 "bensampo/laravel-enum": "^4.2", 9 "bensampo/laravel-enum": "^4.2",
10 "beyondcode/laravel-websockets": "^1.14", 10 "beyondcode/laravel-websockets": "^1.14",
11 "doctrine/dbal": "^3.6", 11 "doctrine/dbal": "^3.6",
  1 +<?php
  2 +
  3 +return [
  4 + /*
  5 + |--------------------------------------------------------------------------
  6 + | HTTP server configurations.
  7 + |--------------------------------------------------------------------------
  8 + |
  9 + | @see https://www.swoole.co.uk/docs/modules/swoole-server/configuration
  10 + |
  11 + */
  12 + 'server' => [
  13 + 'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
  14 + 'port' => env('SWOOLE_HTTP_PORT', '1215'),
  15 + 'public_path' => base_path('public'),
  16 + // Determine if to use swoole to respond request for static files
  17 + 'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
  18 + 'access_log' => env('SWOOLE_HTTP_ACCESS_LOG', false),
  19 + // You must add --enable-openssl while compiling Swoole
  20 + // Put `SWOOLE_SOCK_TCP | SWOOLE_SSL` if you want to enable SSL
  21 + 'socket_type' => SWOOLE_SOCK_TCP,
  22 + 'process_type' => SWOOLE_PROCESS,
  23 + 'options' => [
  24 + 'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
  25 + 'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
  26 + 'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
  27 + // Normally this value should be 1~4 times larger according to your cpu cores.
  28 + 'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num()),
  29 + 'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num()),
  30 + 'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num()),
  31 + // The data to receive can't be larger than buffer_output_size.
  32 + 'package_max_length' => 20 * 1024 * 1024,
  33 + // The data to send can't be larger than buffer_output_size.
  34 + 'buffer_output_size' => 10 * 1024 * 1024,
  35 + // Max buffer size for socket connections
  36 + 'socket_buffer_size' => 128 * 1024 * 1024,
  37 + // Worker will restart after processing this number of requests
  38 + 'max_request' => 3000,
  39 + // Enable coroutine send
  40 + 'send_yield' => true,
  41 + // You must add --enable-openssl while compiling Swoole
  42 + 'ssl_cert_file' => null,
  43 + 'ssl_key_file' => null,
  44 + ],
  45 + ],
  46 +
  47 + /*
  48 + |--------------------------------------------------------------------------
  49 + | Enable to turn on websocket server.
  50 + |--------------------------------------------------------------------------
  51 + */
  52 + 'websocket' => [
  53 + 'enabled' => env('SWOOLE_HTTP_WEBSOCKET', true),
  54 + ],
  55 +
  56 + /*
  57 + |--------------------------------------------------------------------------
  58 + | Hot reload configuration
  59 + |--------------------------------------------------------------------------
  60 + */
  61 + 'hot_reload' => [
  62 + 'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', false),
  63 + 'recursively' => env('SWOOLE_HOT_RELOAD_RECURSIVELY', true),
  64 + 'directory' => env('SWOOLE_HOT_RELOAD_DIRECTORY', base_path()),
  65 + 'log' => env('SWOOLE_HOT_RELOAD_LOG', true),
  66 + 'filter' => env('SWOOLE_HOT_RELOAD_FILTER', '.php'),
  67 + ],
  68 +
  69 + /*
  70 + |--------------------------------------------------------------------------
  71 + | Console output will be transferred to response content if enabled.
  72 + |--------------------------------------------------------------------------
  73 + */
  74 + 'ob_output' => env('SWOOLE_OB_OUTPUT', true),
  75 +
  76 + /*
  77 + |--------------------------------------------------------------------------
  78 + | Pre-resolved instances here will be resolved when sandbox created.
  79 + |--------------------------------------------------------------------------
  80 + */
  81 + 'pre_resolved' => [
  82 + 'view', 'files', 'session', 'session.store', 'routes',
  83 + 'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookie',
  84 + 'encrypter', 'hash', 'router', 'translator', 'url', 'log',
  85 + ],
  86 +
  87 + /*
  88 + |--------------------------------------------------------------------------
  89 + | Instances here will be cleared on every request.
  90 + |--------------------------------------------------------------------------
  91 + */
  92 + 'instances' => [
  93 + 'auth',
  94 + ],
  95 +
  96 + /*
  97 + |--------------------------------------------------------------------------
  98 + | Providers here will be registered on every request.
  99 + |--------------------------------------------------------------------------
  100 + */
  101 + 'providers' => [
  102 + Illuminate\Pagination\PaginationServiceProvider::class,
  103 + ],
  104 +
  105 + /*
  106 + |--------------------------------------------------------------------------
  107 + | Resetters for sandbox app.
  108 + |--------------------------------------------------------------------------
  109 + */
  110 + 'resetters' => [
  111 + SwooleTW\Http\Server\Resetters\ResetConfig::class,
  112 + SwooleTW\Http\Server\Resetters\ResetSession::class,
  113 + SwooleTW\Http\Server\Resetters\ResetCookie::class,
  114 + SwooleTW\Http\Server\Resetters\ClearInstances::class,
  115 + SwooleTW\Http\Server\Resetters\BindRequest::class,
  116 + SwooleTW\Http\Server\Resetters\RebindKernelContainer::class,
  117 + SwooleTW\Http\Server\Resetters\RebindRouterContainer::class,
  118 + SwooleTW\Http\Server\Resetters\RebindViewContainer::class,
  119 + SwooleTW\Http\Server\Resetters\ResetProviders::class,
  120 + ],
  121 +
  122 + /*
  123 + |--------------------------------------------------------------------------
  124 + | Define your swoole tables here.
  125 + |
  126 + | @see https://www.swoole.co.uk/docs/modules/swoole-table
  127 + |--------------------------------------------------------------------------
  128 + */
  129 + 'tables' => [
  130 + // 'table_name' => [
  131 + // 'size' => 1024,
  132 + // 'columns' => [
  133 + // ['name' => 'column_name', 'type' => Table::TYPE_STRING, 'size' => 1024],
  134 + // ]
  135 + // ],
  136 + ],
  137 +];
  1 +<?php
  2 +
  3 +return [
  4 + /*
  5 + |--------------------------------------------------------------------------
  6 + | Websocket handler for onOpen and onClose callback
  7 + | Replace this handler if you want to customize your websocket handler
  8 + |--------------------------------------------------------------------------
  9 + */
  10 + 'handler' => SwooleTW\Http\Websocket\SocketIO\WebsocketHandler::class,
  11 +
  12 + /*
  13 + |--------------------------------------------------------------------------
  14 + | Default frame parser
  15 + | Replace it if you want to customize your websocket payload
  16 + |--------------------------------------------------------------------------
  17 + */
  18 + 'parser' => SwooleTW\Http\Websocket\SocketIO\SocketIOParser::class,
  19 +
  20 + /*
  21 + |--------------------------------------------------------------------------
  22 + | Websocket route file path
  23 + |--------------------------------------------------------------------------
  24 + */
  25 + 'route_file' => base_path('routes/websocket.php'),
  26 +
  27 + /*
  28 + |--------------------------------------------------------------------------
  29 + | Default middleware for on connect request
  30 + |--------------------------------------------------------------------------
  31 + */
  32 + 'middleware' => [
  33 + // SwooleTW\Http\Websocket\Middleware\DecryptCookies::class,
  34 + // SwooleTW\Http\Websocket\Middleware\StartSession::class,
  35 + // SwooleTW\Http\Websocket\Middleware\Authenticate::class,
  36 + ],
  37 +
  38 + /*
  39 + |--------------------------------------------------------------------------
  40 + | Websocket handler for customized onHandShake callback
  41 + |--------------------------------------------------------------------------
  42 + */
  43 + 'handshake' => [
  44 + 'enabled' => false,
  45 + 'handler' => SwooleTW\Http\Websocket\HandShakeHandler::class,
  46 + ],
  47 +
  48 + /*
  49 + |--------------------------------------------------------------------------
  50 + | Default websocket driver
  51 + |--------------------------------------------------------------------------
  52 + */
  53 + 'default' => 'table',
  54 +
  55 + /*
  56 + |--------------------------------------------------------------------------
  57 + | Websocket client's heartbeat interval (ms)
  58 + |--------------------------------------------------------------------------
  59 + */
  60 + 'ping_interval' => 25000,
  61 +
  62 + /*
  63 + |--------------------------------------------------------------------------
  64 + | Websocket client's heartbeat interval timeout (ms)
  65 + |--------------------------------------------------------------------------
  66 + */
  67 + 'ping_timeout' => 60000,
  68 +
  69 + /*
  70 + |--------------------------------------------------------------------------
  71 + | Room drivers mapping
  72 + |--------------------------------------------------------------------------
  73 + */
  74 + 'drivers' => [
  75 + 'table' => SwooleTW\Http\Websocket\Rooms\TableRoom::class,
  76 + 'redis' => SwooleTW\Http\Websocket\Rooms\RedisRoom::class,
  77 + ],
  78 +
  79 + /*
  80 + |--------------------------------------------------------------------------
  81 + | Room drivers settings
  82 + |--------------------------------------------------------------------------
  83 + */
  84 + 'settings' => [
  85 +
  86 + 'table' => [
  87 + 'room_rows' => 4096,
  88 + 'room_size' => 2048,
  89 + 'client_rows' => 8192,
  90 + 'client_size' => 2048,
  91 + ],
  92 +
  93 + 'redis' => [
  94 + 'server' => [
  95 + 'host' => env('REDIS_HOST', '127.0.0.1'),
  96 + 'password' => env('REDIS_PASSWORD', null),
  97 + 'port' => env('REDIS_PORT', 6379),
  98 + 'database' => 0,
  99 + 'persistent' => true,
  100 + ],
  101 + 'options' => [
  102 + //
  103 + ],
  104 + 'prefix' => 'swoole:',
  105 + ],
  106 + ],
  107 +];