web.php
use Symfony\Component\Console\Output\NullOutput;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
app()->singleton(WebsocketsLogger::class, function () {
return (new WebsocketsLogger(new NullOutput()))->enable(false);
});
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
WebSocketsRouter::webSocket('/my-websocket', \App\MyCustomWebSocketHandler::class);
MyCustomWebSocketHandler
<?php
namespace App;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
class MyCustomWebSocketHandler extends WebSocketHandler
{
public function onMessage(ConnectionInterface $connection, MessageInterface $msg)
{
// TODO: Implement onMessage() method.
}
}
客户
let socket = new WebSocket("ws://localhost:6001/app/websocketkey/my-websocket?protocol=7&client=js&version=4.3.1&flash=false");
尝试运行客户端连接时,它未连接到自定义处理程序。默认处理程序运行正常。
Websocket:serve
和artisan:serve
正在运行
对此有任何想法吗? Tried with the demo here
答案 0 :(得分:1)
按如下所示设置网络套接字路由即可解决此问题。
use Symfony\Component\Console\Output\NullOutput;
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
app()->singleton(WebsocketsLogger::class, function () {
return (new WebsocketsLogger(new NullOutput()))->enable(false);
});
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
WebSocketsRouter::webSocket('/app/{appkey}/my-websocket', \App\MyCustomWebSocketHandler::class);