作者 张关杰

gx

... ... @@ -10,27 +10,19 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class WebSocketMessage
class WebSocketMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
public $message;
public function __construct($message)
{
//
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
return new Channel('websocket-channel');
}
}
... ...
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class WebSocketMessageSent implements ShouldBroadcast
{
use Dispatchable, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('websocket-channel');
}
}
... ... @@ -10,6 +10,7 @@
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Events\WebSocketMessage;
use App\Events\WebSocketMessageSent;
use App\Helper\Common;
use App\Helper\Translate;
... ... @@ -146,7 +147,7 @@ class LoginController extends BaseController
// $message = file_get_contents("php://input");
// $message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
// @file_put_contents(storage_path('logs/lyh_error.log'), $message . PHP_EOL, FILE_APPEND);
event(new WebSocketMessageSent('New message from server'));
event(new WebSocketMessage('Hello, WebSocket!'));
$this->response('success');
}
... ...
... ... @@ -5,7 +5,7 @@
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^7.4|^8.1",
i
"bensampo/laravel-enum": "^4.2",
"beyondcode/laravel-websockets": "^1.14",
"doctrine/dbal": "^3.6",
... ...