Why is socket.setNoDelay() throwing an error?

2019-08-14 04:12发布

问题:

I have a nodejs server using socketio for real-time comms.

var app = require('http').createServer(handler)
  , io = require('socket.io')().listen(app)

function init() {
    app.listen(process.env.PORT || 8888);
};

io.sockets.on('connection', function (socket) {
    socket.setNoDelay(true);
    onSocketConnection(socket);
});

Issue is, every time I call socket.setNoDelay(true); it's kicking back:

E:[path]\server.js:58
  socket.setNoDelay(true);
         ^
TypeError: undefined is not a function
    at Namespace.<anonymous> (E:\[path]\server.js:58:12)   
    at Namespace.emit (events.js:107:17)
    at Namespace.emit (E:\path]\node_modules\socket.io\lib\namespace.js:205:10)   
    at E:\[path]\node_modules\socket.io\lib\namespace.js:172:14
    at process._tickCallback (node.js:355:11)

I can't seem to find any documentation as to why this is happening. Anyone else see this?

Specs:

> Windows Environment  
> node version: 0.12.4 
> socket.io version: 1.3.6

回答1:

Because socket isn't a net.Socket, it's a socket.io Socket. You'll notice that there is no setNoDelay method on a socket.io Socket.

The socket.io websocket sever automatically disables Nagle on the underlying TCP socket.