英文:
Laravel WebSocket get SSL error message though Pusher
问题
I'm using Websocket with SSL but if SSL has any issue I'm not able to find out the exact issue. After investigation, "SecureServer.php" has a code that emits the error, but that error is not getting pushed through Pusher.
This is the code in SecureServer.php:
With a valid SSL certificate and private key, it's working fine. But I need to show an error when there is an issue.
Can anyone help me to get this emitted error message through Pusher?
Versions:
- Laravel: 9.52.4
- laravel-websockets: 1.13.2
- react/socket: 1.12.0
- pusher-js: 7.0.3
I've created a custom WebSocketHandler sending "pusher:error" from the onError()
function:
public function onError(ConnectionInterface $connection, Exception $exception)
{
if ($exception instanceof WebSocketException) {
$connection->send(
json_encode([
"event" => "pusher:error",
"data" => json_encode([
"socket_id" => $connection->socketId,
"activity_timeout" => 30,
"exception" => json_encode($exception->getPayload()),
]),
])
);
}
}
But it didn't work.
英文:
I'm using Websocket with SSL but if SSL has any issue I'm not able to findout out the exact issue. After Investigation "SecureServer.php" has a code which are emit the error but that error is not getting through pusher.
This is the code in SecureServer.php
With a valid SSL (certificate & private key) it's working fine. but I need to show an error when there is issue
can anyone help me on this to get this emitted error message through pusher?
Versions:
- Laravel: 9.52.4
- laravel-websockets: 1.13.2
- react/socket: 1.12.0
- pusher-js: 7.0.3
I've created a Custom WebSocketHandler sending pusher:error from OnError() function
public function onError(ConnectionInterface $connection, Exception $exception)
{
if ($exception instanceof WebSocketException) {
$connection->send(
json_encode([
"event" => "pusher:error",
"data" => json_encode([
"socket_id" => $connection->socketId,
"activity_timeout" => 30,
"exception" => json_encode($exception->getPayload()),
]),
])
);
}
}
But didn't worked.
答案1
得分: 0
以下是您要翻译的内容:
也许这不是一个优化的解决方案
解决步骤:
步骤 1: 创建另一个命令MyStartWebSocketServer.php
,并从vendor/beyondcode/laravel-websockets/src/Console/StartWebSocketServer.php
复制所有内容,然后更改命令签名从
websockets:serve
到
mywebsockets:serve
步骤 2: 在/app
文件夹下创建一个名为MyWebSocketServerFactory.php
的文件,并复制所有内容从vendor/beyondcode/laravel-websockets/src/Server/WebSocketServerFactory.php
。
步骤 3: 转到MyStartWebSocketServer.php
文件中的第148行,并更改从
(new WebSocketServerFactory())
到
(new MyWebSocketServerFactory())
步骤 4: 在/app
文件夹下创建一个名为MySecureServer.php
的文件,并复制所有内容从vendor/react/socket/src/SecureServer.php
。
步骤 5: 转到MyWebSocketServerFactory.php
文件的第79行,并更改从
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
到
$socket = new MySecureServer($socket, $this->loop, config('websockets.ssl'));
步骤 6: 转到MySecureServer.php
文件的第200行,在连接未形成时记录错误并向用户显示适当的消息。
$this->encryption->enable($connection)->then(
function ($conn) use ($that) {
$that->emit("connection", [$conn]);
},
function ($error) use ($that, $connection, $remote) {
$error = new \RuntimeException("来自" . $remote . "的连接在TLS握手期间失败:" . $error->getMessage(), $error->getCode());
// 在UI上发送错误消息的您的代码
$that->emit("error", [$error]);
$connection->close();
}
);
英文:
Maybe It's not an optimized solution
Solution Steps:
Step 1: Created another command MyStartWebSocketServer.php
& copy all the content from vendor/beyondcode/laravel-websockets/src/Console/StartWebSocketServer.php
And change command signature from
websockets:serve
to
mywebsockets:serve
Step 2: Created another file under /app
folder with a name MyWebSocketServerFactory.php
& copy all content from vendor/beyondcode/laravel-websockets/src/Server/WebSocketServerFactory.php
Step 3: Goto Line Number 148 in MyStartWebSocketServer.php
file & changed from
(new WebSocketServerFactory())
to
(new MyWebSocketServerFactory())
Step 4: Created another file under /app
folder with a name MySecureServer.php
& copy all content from vendor/react/socket/src/SecureServer.php
Step 5: Goto Line Number 79 in MyWebSocketServerFactory.php
file & changed from
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
to
$socket = new MySecureServer($socket, $this->loop, config('websockets.ssl'));
Step 6: Goto line 200 in MySecureServer.php
file and log the error when connection is not formed & show a proper message to user
$this->encryption->enable($connection)->then(
function ($conn) use ($that) {
$that->emit("connection", [$conn]);
},
function ($error) use ($that, $connection, $remote) {
$error = new \RuntimeException("Connection from " . $remote . " failed during TLS handshake: " . $error->getMessage(), $error->getCode());
// your code to send error message on UI
$that->emit("error", [$error]);
$connection->close();
}
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论