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:
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.
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();
}
);