WebSocket-Node
WebSocket-Node copied to clipboard
WebsocketServer stops writing bytes to clients
Hi,
I am using WebsocketServer
. Sometimes, I see that the server cannot send bytes
over the connection. Although it can still accept more connections from client side, these connections are stuck at responding too!
My websocket
version is 1.0.24
, checked by npm list websocket
.
Below is the simplified model of my server:
const WebsocketServer = require("websocket").server
const https = require("https")
const fs = require("fs")
const options = {
key: fs.readFileSync('/path/mykey.key'),
cert: fs.readFileSync('/path/mycrt.crt'),
ca: fs.readFileSync('/path/bundle.ca-bundle')
}
const server = https.createServer(options, function(req, resp) {
console.log("HTTP request: " + req.connection.remoteAddress)
})
server.listen(PORT, function() { // PORT is a define
console.log((new Date()) + " Server is listening on port " + PORT);
})
const wssServer = new WebsocketServer({
httpServer: server
})
wssServer.on("request", function(request) {
const connection = request.accept(null, request.origin)
console.log("new connection -> fd: " + connection.socket._handle.fd)
connection.on("message", function(message) {
if (message.type !== "binary") {
console.log("data not binary")
connection.close()
return
}
const resp = Buffer.concat([...])
connection.sendBytes(resp) // sometimes it does not work any more: client side does not get any data!
})
connection.on("close", function(code, description) {
console.log((new Date()) + " peer " + connection.remoteAddress + " disconnected. Code: " + code + ", " + description)
console.log("remaining: " + wssServer.connections.length)
})
})
The problem is: this happens very rarely, but it does exist!
Any ideas to fix this weird issue?
Regards,
How can one reproduce this? It sounds more like your clients diconnected or something like that.
How do i change the problem i dont understand it all
This should probably just be closed.
I'm just now running into a problem that sounds similar to the one the poster experienced. I'm using the client. When sending larger data, the socket abruptly closes with error "Socket Error: write EPIPE". Here's what the connection object looks like when this happens:
{ _debug: <function:debug>, _events: { newListener: <function>, removeListener: <function> }, _eventsCount: 2, _maxListeners: undefined, _pingListenerCount: 0, config: { maxReceivedFrameSize: 1048576, maxReceivedMessageSize: 8388608, fragmentOutgoingMessages: true, fragmentationThreshold: 16384, webSocketVersion: 13, assembleFragments: true, disableNagleAlgorithm: true, closeTimeout: 5000, tlsOptions: { } }, socket: { _tlsOptions: { pipe: false, secureContext: { context: null, singleUse: true }, isServer: false, requestCert: true, rejectUnauthorized: true, session: undefined, NPNProtocols: undefined, ALPNProtocols: undefined, requestOCSP: undefined }, _secureEstablished: true, _securePending: false, _newSessionPending: false, _controlReleased: true, _SNICallback: null, servername: "www.example.com", npnProtocol: false, alpnProtocol: false, authorized: true, authorizationError: null, encrypted: true, _events: { close: [ <function:onSocketCloseDestroySSL>, <function:bound > ], finish: <function:onSocketFinish>, end: [ <function:onReadableStreamEnd>, <function:bound > ], secure: <function:onConnectSecure>, error: <function:bound >, drain: <function:bound >, pause: <function:bound >, resume: <function:bound >, data: <function:bound > }, _eventsCount: 9, connecting: false, _hadError: false, _handle: null, _parent: null, _host: "www.example.com", _readableState: { objectMode: false, highWaterMark: 16384, buffer: { head: null, tail: null, length: 0 }, length: 0, pipes: null, pipesCount: 0, flowing: true, ended: true, endEmitted: true, reading: false, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, destroyed: true, defaultEncoding: "utf8", awaitDrain: 0, readingMore: false, decoder: null, encoding: null }, readable: false, _maxListeners: undefined, _writableState: { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: true, ending: true, ended: true, finished: true, destroyed: true, decodeStrings: false, defaultEncoding: "utf8", length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: <function:bound onwrite>, writecb: null, writelen: 0, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: true, errorEmitted: true, bufferedRequestCount: 0, corkedRequestsFree: { next: { next: null, entry: null, finish: <function:bound onCorkedFinish> }, entry: null, finish: <function:bound onCorkedFinish> } }, writable: false, _bytesDispatched: 75273, _sockname: null, _pendingData: null, _pendingEncoding: "", allowHalfOpen: false, server: undefined, _server: null, ssl: null, _requestCert: true, _rejectUnauthorized: true, parser: null, _httpMessage: null, _peername: { address: "127.0.0.1", family: "IPv4", port: 443 }, _idleNext: null, _idlePrev: null, _idleTimeout: -1, write: <function:writeAfterFIN> }, protocol: undefined, extensions: [], remoteAddress: "127.0.0.1", closeReasonCode: 1006, closeDescription: "Socket Error: write EPIPE", closeEventEmitted: true, maskOutgoingPackets: true, maskBytes: [209, 79, 155, 204], frameHeader: [136, 0, 0, 0, 0, 0, 0, 0, 0, 0], bufferList: { _events: { }, _eventsCount: 0, _maxListeners: undefined, encoding: undefined, construct: <function:Buffer>, length: 12, write: <function>, end: <function>, push: <function>, forEach: <function>, join: <function>, joinInto: <function>, advance: <function>, take: <function>, toString: <function> }, currentFrame: { maskBytes: [209, 79, 155, 204], frameHeader: [136, 0, 0, 0, 0, 0, 0, 0, 0, 0], config: { maxReceivedFrameSize: 1048576, maxReceivedMessageSize: 8388608, fragmentOutgoingMessages: true, fragmentationThreshold: 16384, webSocketVersion: 13, assembleFragments: true, disableNagleAlgorithm: true, closeTimeout: 5000, tlsOptions: { } }, maxReceivedFrameSize: 1048576, protocolError: false, frameTooLarge: false, invalidCloseFrameLength: false, parseState: 1, closeStatus: -1 }, fragmentationSize: 0, frameQueue: [], connected: false, state: "closed", waitingForCloseResponse: false, receivedEnd: true, closeTimeout: 5000, assembleFragments: true, maxReceivedMessageSize: 8388608, outputBufferFull: true, inputPaused: false, receivedDataHandler: <function:bound >, _closeTimerHandler: <function:bound >, webSocketVersion: 13, socketHadError: true
I'm using NChan as the server and it has no problem sending out the same packets that come to it via a traditional POST request, so I can only think that the Websocket Client package has an issue.