作者 张关杰

gx

@@ -10,27 +10,19 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast; @@ -10,27 +10,19 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10 use Illuminate\Foundation\Events\Dispatchable; 10 use Illuminate\Foundation\Events\Dispatchable;
11 use Illuminate\Queue\SerializesModels; 11 use Illuminate\Queue\SerializesModels;
12 12
13 -class WebSocketMessage 13 +class WebSocketMessage implements ShouldBroadcast
14 { 14 {
15 use Dispatchable, InteractsWithSockets, SerializesModels; 15 use Dispatchable, InteractsWithSockets, SerializesModels;
16 16
17 - /**  
18 - * Create a new event instance.  
19 - *  
20 - * @return void  
21 - */  
22 - public function __construct() 17 + public $message;
  18 +
  19 + public function __construct($message)
23 { 20 {
24 - // 21 + $this->message = $message;
25 } 22 }
26 23
27 - /**  
28 - * Get the channels the event should broadcast on.  
29 - *  
30 - * @return \Illuminate\Broadcasting\Channel|array  
31 - */  
32 public function broadcastOn() 24 public function broadcastOn()
33 { 25 {
34 - return new PrivateChannel('channel-name'); 26 + return new Channel('websocket-channel');
35 } 27 }
36 } 28 }
1 -<?php  
2 -  
3 -namespace App\Events;  
4 -  
5 -use Illuminate\Broadcasting\Channel;  
6 -use Illuminate\Contracts\Broadcasting\ShouldBroadcast;  
7 -use Illuminate\Foundation\Events\Dispatchable;  
8 -use Illuminate\Queue\SerializesModels;  
9 -  
10 -class WebSocketMessageSent implements ShouldBroadcast  
11 -{  
12 - use Dispatchable, SerializesModels;  
13 -  
14 - public $message;  
15 -  
16 - public function __construct($message)  
17 - {  
18 - $this->message = $message;  
19 - }  
20 -  
21 - public function broadcastOn()  
22 - {  
23 - return new Channel('websocket-channel');  
24 - }  
25 -}  
@@ -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",