You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constnet=require('net');conststream=require('stream');constdata=Buffer.alloc(15*1024*1024);constserver=net.createServer(function(socket){functiononData(chunk){if(!writable.write(chunk)){socket.pause();}}constwritable=newstream.Writable({write(chunk,encoding,callback){callback(newError('Oops'));}});writable.on('error',function(err){console.error(err);socket.end();socket.removeListener('data',onData);socket.resume();});socket.on('data',onData);socket.on('end',function(){console.log('server end');});});server.listen(function(){const{ port }=server.address();constsocket=net.createConnection({ port });socket.on('connect',function(){socket.write(data);});socket.on('end',function(){console.log('client end');});socket.resume();});
// this program:// * starts an http server on port 8545// * the http server hands off WebSocket upgrades to a `ws` Server// * then sends a WebSocket payload to the server that is over it's `maxPayload` size// * it "handles" the server-side `ws` error by listening to the "error" event// * it expects the WebSocket connection to close immediately, but on node 10-12 it doesn'tconsthttp=require("http");constWebSocket=require("ws");constMAX_PAYLOAD_SIZE=15*1024*1024;// an arbitrary sizeconstPORT=8545;constserver=http.createServer();constwebsocketServer=newWebSocket.Server({noServer: true,// we want to use our own `http` servermaxPayload: MAX_PAYLOAD_SIZE,});functionhandleHttpUpgrade(request,socket,head){websocketServer.handleUpgrade(request,socket,head,(ws,incomingMessage)=>{// handle websocket errors, like "Max payload size exceeded":ws.on("error",(error)=>{console.log("ws error",error);console.log("ws errored at",newDate());});ws.on("close",()=>{// log the current time when the server-side websocket is closedconsole.log("ws closed at",newDate());});});}functionlistening(){constclientWebsocket=newWebSocket(`ws://127.0.0.1:${PORT}`);clientWebsocket.on("open",()=>{// send too much data:clientWebsocket.send(Buffer.alloc(MAX_PAYLOAD_SIZE+1));});clientWebsocket.on("close",()=>{// log the current time when the client websocket is closedconsole.log("clientWebsocket closed at",newDate());});}server.on("upgrade",handleHttpUpgrade);server.listen({port: PORT},listening);
This discussion was converted from issue #3 on March 22, 2022 00:32.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Saving a reference to websockets#1940 (comment)
For this problem: websockets#1940 (comment)
Beta Was this translation helpful? Give feedback.
All reactions