-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I host ratchet into the internet? #1074
Comments
hello, |
Hi, I have resolved it but it only works in http and not in SSL. I wasn't able to proxy it using SSL. |
@amiel0606 First, tell what proxy server you use and show your proxy and ratchet config |
clients = new \SplObjectStorage(); $this->sessionClients = []; } public function onOpen(ConnectionInterface $conn) { $queryParams = []; parse_str($conn->httpRequest->getUri()->getQuery(), $queryParams); $sessionId = $queryParams['session_id'] ?? null; if ($sessionId) { $this->clients->attach($conn); $this->sessionClients[$sessionId] = $conn; echo "New connection (Session ID: $sessionId, Connection ID: {$conn->resourceId})\n"; } else { echo "Connection rejected: No session ID provided.\n"; $conn->close(); } } public function onMessage(ConnectionInterface $from, $msg) { echo "Received message: $msg\n"; $data = json_decode($msg, true); if (json_last_error() !== JSON_ERROR_NONE) { echo "Invalid JSON received.\n"; return; } if (!isset($data['channel'])) { echo "No channel specified in the message.\n"; return; } if (isset($data['users'])) { $users = $data['users']; foreach ($users as $userId) { if (isset($this->sessionClients[$userId])) { if ($userId != $data['senderUserId']) { $this->sessionClients[$userId]->send($msg); echo "Message sent to Session ID: $userId\n"; } } else { echo "Session ID: $userId is not connected.\n"; } } } else { foreach ($this->clients as $client) { $client->send($msg); } } } public function onClose(ConnectionInterface $conn) { foreach ($this->sessionClients as $sessionId => $clientConn) { if ($clientConn === $conn) { unset($this->sessionClients[$sessionId]); echo "Connection closed (Session ID: $sessionId, Connection ID: {$conn->resourceId})\n"; break; } } $this->clients->detach($conn); } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error occurred: {$e->getMessage()}\n"; $conn->close(); } } // Create an event loop $loop = EventLoopFactory::create(); echo "Starting WebSocket server with SSL...\n"; // Create a socket server $socketServer = new SocketServer('0.0.0.0:8081', [], $loop); // Wrap it with SSL try { $secureSocket = new SecureServer($socketServer, $loop, [ 'local_cert' => 'certificates/fullchain.pem', 'local_pk' => certificates/privkey.pem', 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER | STREAM_CRYPTO_METHOD_TLSv1_3_SERVER, 'verify_peer' => false, ]); } catch (\Exception $e) { echo "SecureServer error: " . $e->getMessage() . "\n"; exit(1); } // Start the WebSocket server $server = new IoServer( new HttpServer( new WsServer( new NotificationServer() ) ), $secureSocket, $loop ); echo "WebSocket server started\n"; $loop->run(); |
I am using apache with php on cpanel ubuntu |
OK, here is a config for a secure websocket connection with Apache: #we must add special headers to the websocket request
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:8080/$1 [P,L]
#redirect an external request to our local websocket server
ProxyPass /websocket ws://localhost:8080
ProxyPassReverse /websocket ws://localhost:8080 This code should be added to your Apache's ssl conf file. By default, it's located at |
Sorry if this question is already asked but I am stuck with this problem for 2 days now, I need to host my website into a domain, it is all working but the websocket connect is not established. I have purchased a VPS already but it is not still working and I do not know what to do
The text was updated successfully, but these errors were encountered: