Closed programarivm closed 5 months ago
Hi Jordi Bassagana
new (‘websocket://0.0.0.0:2346’);
------------------ Original ------------------ From: Jordi Bassagana @.> Date: Tue,Jan 30,2024 5:41 PM To: walkor/workerman @.> Cc: Subscribed @.***> Subject: Re: [walkor/workerman] Error package because of receiving data beforehandshake (Issue #1007)
Hi there,
I'm trying to run a basic WebSocket server as described in the README.md. <?php use Workerman\Worker; require DIR . '/../../vendor/autoload.php'; // Create a Websocket server $ws_worker = new Worker('ws://0.0.0.0:2346'); // Emitted when new connection come $ws_worker->onConnect = function ($connection) { echo "New connection\n"; }; // Emitted when data received $ws_worker->onMessage = function ($connection, $data) { // Send hello $data $connection->send('Hello ' . $data); }; // Emitted when connection closed $ws_worker->onClose = function ($connection) { echo "Connection closed\n"; }; // Run worker Worker::runAll();
The WebSocket server is started OK.
php cli/workerman/example.php start Workerman[cli/workerman/example.php] start in DEBUG mode ------------------------------------------- WORKERMAN -------------------------------------------- Workerman version:4.1.14 PHP version:8.3.1 Event-Loop:\Workerman\Events\Select -------------------------------------------- WORKERS --------------------------------------------- proto user worker listen processes status tcp standard none ws://0.0.0.0:2346 1 [OK] -------------------------------------------------------------------------------------------------- Press Ctrl+C to stop. Start success.
However, it'll throw a package error because of receiving data before the handshake.
php cli/workerman/example.php start Workerman[cli/workerman/example.php] start in DEBUG mode ------------------------------------------- WORKERMAN -------------------------------------------- Workerman version:4.1.14 PHP version:8.3.1 Event-Loop:\Workerman\Events\Select -------------------------------------------- WORKERS --------------------------------------------- proto user worker listen processes status tcp standard none ws://0.0.0.0:2346 1 [OK] -------------------------------------------------------------------------------------------------- Press Ctrl+C to stop. Start success. New connection recv data before handshake. Buffer:474554202f20485454502f312e310d0a486f73743a203132372e302e302e313a323334360d0a436f6e6e656374696f6e3a20557067726164650d0a507261676d613a206e6f2d63616368650d0a43616368652d436f6e74726f6c3a206e6f2d63616368650d0a557365722d4167656e743a204d6f7a696c6c612f352e3020285831313b204c696e7578207838365f363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f3132302e302e302e30205361666172692f3533372e33360d0a557067726164653a20776562736f636b65740d0a4f726967696e3a2068747470733a2f2f63686573732d7365727665722e646f63732e63686573736c61626c61622e6f72670d0a5365632d576562536f636b65742d56657273696f6e3a2031330d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174652c2062720d0a4163636570742d4c616e67756167653a20656e2d55532c656e3b713d302e390d0a5365632d576562536f636b65742d4b65793a20714c4e545141576e583339676c65477031372f7543513d3d0d0a5365632d576562536f636b65742d457874656e73696f6e733a207065726d6573736167652d6465666c6174653b20636c69656e745f6d61785f77696e646f775f626974730d0a0d0a Error package. package_length=falseConnection closed
By the way, I'm trying to connect to the WebSocket server through the JavaScript console as shown in the image below.
Screenshot.2024-01-30.10.26.45.png (view on web)
WebSocket connection to 'ws://127.0.0.1:2346/' failed:
Any help will be very much appreciated!
Thank you,
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>
Thanks for the response,
const ws = new WebSocket('websocket://127.0.0.1:2346');
It seems though as if the websocket
protocol can't be used in the JavaScript code.
Uncaught DOMException: Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'websocket' is not allowed.
at <anonymous>:1:12
The URL's scheme must be either ws
or wss
.
walkor means modify in PHP, not JS.
<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// Create a Websocket server
$ws_worker = new Worker('websocket://0.0.0.0:2346');
// Emitted when new connection come
$ws_worker->onConnect = function ($connection) {
echo "New connection\n";
};
// Emitted when data received
$ws_worker->onMessage = function ($connection, $data) {
// Send hello $data
$connection->send('Hello ' . $data);
};
// Emitted when connection closed
$ws_worker->onClose = function ($connection) {
echo "Connection closed\n";
};
// Run worker
Worker::runAll();
let socket = new WebSocket("ws://127.0.0.1:2346");
socket.onopen = function(e) {
alert("[open] Connection established");
alert("Sending to server");
socket.send("My name is John");
};
socket.onmessage = function(event) {
alert(`[message] Data received from server: ${event.data}`);
};
socket.onclose = function(event) {
if (event.wasClean) {
alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// 例如服务器进程被杀死或网络中断
// 在这种情况下,event.code 通常为 1006
alert('[close] Connection died');
}
};
socket.onerror = function(error) {
alert(`[error] ${error.message}`);
};
Hi there,
I'm trying to run a basic WebSocket server as described in the
README.md
.The WebSocket server is started
OK
.However, it'll throw a package error because of receiving data before the handshake.
By the way, I'm trying to connect to the WebSocket server through the JavaScript console as shown in the image below.
Any help will be very much appreciated!
Thank you,