Skip to content
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

Invalid WebSocket frame #1315

Closed
imbundle opened this issue Mar 4, 2018 · 8 comments
Closed

Invalid WebSocket frame #1315

imbundle opened this issue Mar 4, 2018 · 8 comments

Comments

@imbundle
Copy link

imbundle commented Mar 4, 2018

const WebSocket = require('ws');
const express = require('express');
const bodyParser = require('body-parser');
const url = require('url');
const http = require('http');
const app = express();
app.use(bodyParser.urlencoded({
    extended: true,
}));
app.use(bodyParser.json());

const server = http.createServer(app);
server.listen(2015,'127.0.0.1');
const wsScreen = new WebSocket.Server({ server });
wsScreen.on('connection', (ws, request) => {
    ws.send("HI BIG SCREEN");
});

const wsClient = new WebSocket.Server({ server });
wsClient.on('connection', (ws, request) => {
     ws.send("HI CLIENT");
});
server.on('upgrade', (request, socket, head) => {
    const { pathname } = url.parse(request.url);

    console.log('PATHNAME', pathname);
    if (pathname === '/client') {
        wsClient.handleUpgrade(request, socket, head, (ws) => {
            console.log('New Client connection');
            wsClient.emit('connection', ws, request);
        });
    } else if (pathname === '/screen') {
        wsScreen.handleUpgrade(request, socket, head, (ws) => {
            wsScreen.emit('connection', ws, request);
        });
    } else {
        socket.destroy();
    }
});

Description

I'have a problem whit multi socket
I'have see issue 885

when try to connect to "ws://localhost:2015/screen"

node error :
RangeError: Invalid WebSocket frame

browser(chome) error
WebSocket connection to 'ws://localhost:2015/screen' failed: Invalid frame header

version:
Node.js version(s): 9.4.0
OS version(s): MacOs

@lpinca
Copy link
Member

lpinca commented Mar 4, 2018

Replace const wsScreen = new WebSocket.Server({ server }); with const wsScreen = new WebSocket.Server({ noServer: true }) and similarly for the other server.

@imbundle
Copy link
Author

imbundle commented Mar 5, 2018

Thanks It work

@imbundle imbundle closed this as completed Mar 5, 2018
@gobindabd
Copy link

Getting same error . Any idea .

[root@webrtc callback]# node server.js
got connection
events.js:292
throw er; // Unhandled 'error' event
^

RangeError: Invalid WebSocket frame: MASK must be set
at Receiver.getInfo (/usr/local/node_programs/callback/node_modules/ws/lib/receiver.js:234:16)
at Receiver.startLoop (/usr/local/node_programs/callback/node_modules/ws/lib/receiver.js:130:22)
at Receiver._write (/usr/local/node_programs/callback/node_modules/ws/lib/receiver.js:77:10)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at Receiver.Writable.write (_stream_writable.js:318:11)
at Socket.socketOnData (/usr/local/node_programs/callback/node_modules/ws/lib/websocket.js:875:35)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
Emitted 'error' event on WebSocket instance at:
at Receiver.receiverOnError (/usr/local/node_programs/callback/node_modules/ws/lib/websocket.js:780:13)
at Receiver.emit (events.js:315:20)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:418:5)
at onwrite (_stream_writable.js:445:5)
at Receiver.startLoop (/usr/local/node_programs/callback/node_modules/ws/lib/receiver.js:151:5)
at Receiver._write (/usr/local/node_programs/callback/node_modules/ws/lib/receiver.js:77:10)
[... lines matching original stack trace ...]
at Receiver.Writable.write (_stream_writable.js:318:11) {
[Symbol(status-code)]: 1002
}
[root@webrtc callback]# cat server.js

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });
var fs = require('fs');
var wstream = fs.createWriteStream('audio.raw');

wss.on('connection', function connection(ws) {
console.log("got connection ");

ws.on('message', function incoming(message) {
console.log('received frame..');
wstream.write(message);
});
});

@Esqarrouth
Copy link
Contributor

Esqarrouth commented Jul 4, 2021

Replace const wsScreen = new WebSocket.Server({ server }); with const wsScreen = new WebSocket.Server({ noServer: true }) and similarly for the other server.

Why is this downvoted to oblivion?

@danieltroger
Copy link

Why is this upvoted to oblivion?

Do you mean downvoted? For me it's downvoted

@Esqarrouth
Copy link
Contributor

Yes, downvoted 😅

@DTQX
Copy link

DTQX commented Dec 24, 2021

Replace const wsScreen = new WebSocket.Server({ server }); with const wsScreen = new WebSocket.Server({ noServer: true }) and similarly for the other server.

This method doesn't work for me. My ws version is 7.4.2. And When I upgrade ws version to 8.4.0, the problem is disappear.

@zadam
Copy link

zadam commented Nov 3, 2022

0.56.2 uses ws 8.9.0, so that might be worth a try.

I wonder if it's caused by some specific clients sending weird/invalid WS data frames...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants