Closed ru-zxr closed 1 month ago
I attempted to test the upgrade using core modules following the instructions provided in https://github.com/websockets/ws/issues/1343#issuecomment-377969496, but encountered a problem where the upgrade is not triggered as expected.
It means that the request is not hitting the server. Is the server listening? Is there a proxy in the middle? I have no clue.
I attempted to test the upgrade using core modules following the instructions provided in #1343 (comment), but encountered a problem where the upgrade is not triggered as expected.
It means that the request is not hitting the server. Is the server listening? Is there a proxy in the middle? I have no clue.
I created a create-react-app to test all three options, and all function correctly. However, when I use it in a React Native Expo app, only when specifying a different port, the message 'It works'
is printed.
The socket client upgrade script prints: Error making request: Error: connect ECONNREFUSED local_ip_address:80
when usingnew WebSocket.Server({ noServer: true })
and new WebSocket.Server({ server })
Is the server listening on port 80? You might need elevated privileges to make the server listen on port 80.
Is the server listening on port 80? You might need elevated privileges to make the server listen on port 80.
I need to specify the port as 80: const wss = new WebSocket.Server({ port: 80 })
. This still creates another TCP server.
I don't think it is a ws
issue. Both of the examples below work as expected.
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ server });
wss.on('connection', function (ws) {
ws.send('Hello');
});
server.listen(80, function () {
const ws = new WebSocket('ws://127.0.0.1');
ws.on('open', function () {
console.log('Open');
});
ws.on('message', function (buf) {
console.log(buf.toString());
});
});
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
wss.on('connection', function (ws) {
ws.send('Hello');
});
server.on('upgrade', function (request, socket, head) {
wss.handleUpgrade(request, socket, head, function (ws) {
wss.emit('connection', ws);
});
});
server.listen(80, function () {
const ws = new WebSocket('ws://127.0.0.1');
ws.on('open', function () {
console.log('Open');
});
ws.on('message', function (buf) {
console.log(buf.toString());
});
});
It was my mistake. I was using app.listen()
instead of server.listen()
. The app.listen() method creates and starts a new HTTP server, which does not have the WebSocket server attached to it. Thanks for the support.
Is there an existing issue for this?
Description
Frontend: React Native with Expo Backend: Node.js with Koa
I encountered difficulties establishing a WebSocket connection. I tried all three options provided by WebSocket to create a new server instance. Only initializing the server on a different port (code block 3 below) works. For options 1 and 2, I receive the following error in the frontend socket.onerror:
{"message": "Received bad response code from server: 404."}
1. const wss = new WebSocket.Server({ server })
2. const wss = new WebSocket.Server({ noServer: true })
3. const wss = new WebSocket.Server({ port: 5002 })
What I've tried:
'It works'
is not printed.const request = http.get({ headers: { 'Sec-WebSocket-Key': crypto.randomBytes(16).toString('base64'), 'Sec-WebSocket-Version': 13, 'Connection': 'Upgrade', 'Upgrade': 'websocket' }, hostname: 'local_ip_address', port: '5001' });
request.on('upgrade', function () { console.log('It works.'); });
const wss = new WebSocket.Server({ noServer: true });
server.on("upgrade", (request, socket, head) => { wss.handleUpgrade(request, socket, head, (ws) => { wss.emit("connection", ws, request); }); });