nodejs socket.io with IIS Node

2019-07-25 09:13发布

问题:

I have tried something with node.js in windows vista/IIS 7 using iis node. My idea is to use a server script on the asp .net mvc application to connect to another socket server and serve requests on a persistent fashion.

I downloaded the iisnode version at https://github.com/tjanczuk/iisnode for IIS 7 and able to run the basic 'hello world' http server pipe using http handler mappings in web.config and IIS modules configuration.

I am trying to use socket.io library from node.js. This works independently if I write a server and client. But fails when used with IIS node.

I am having problems going down further. Problems like

  • How to include the npm modules in the asp .net mvc project? I tried putting the node_modules in the folder of node scripts, but that did not help. Basically the require('socket.io') command works, but the socket connection etc., simply fails.
  • How to have a socket connection from client which will keep listening for updates from server(like COMET) - As I said I am trying to use socket.io

Any body tried this before?

回答1:

Reading back a few months ago, WebSocket support is not supported with Socket.IO under IIS, however long polling is.

This was a few months ago, and I'm running up against the same issue now and trying to resolve.

As Tomasz writes:

Please note that iisnode does not support websocket transport, but using socket.io is still possible with other HTTP-based transports like HTTP long polling:

io.configure(function() {
    io.set('transports', ['xhr-polling']);
});

By the looks of it, as of Feb 23, 2012, this functionality is still not supported.

Do you need to go through IIS? Do you have the option of going with a pure Socket.IO/Node option, eliminating IISNode? If you need full WebSocket support with fallback capability, this looks like the only option, unless there are other suggestions?



回答2:

This may or may not be related to your problem. I haven't done much with socket.io, however I was planing to do something very similar to what you're describing. When reading through the source for iisnode, I found the following code in cnodehttpmodule.cpp:

this->applicationManager->GetEventProvider()->Log(L"iisnode received a new http request", WINEVENT_LEVEL_INFO);

// reject websocket connections since iisnode does not support them
// http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#page-17

PCSTR upgrade = pHttpContext->GetRequest()->GetHeader(HttpHeaderUpgrade, NULL);
ErrorIf(upgrade && 0 == strcmp("websocket", upgrade), ERROR_NOT_SUPPORTED); 

It looks to me as though if "websocket" is included in the header of the request, it will be rejected. I would need to read up on the websocket protocol to better understand exactly what this means.

I'll be the first to admit that I don't know overly well how websockets differ from a long running request. As I understand it, however, socket.io will work on older browsers that do not support websockets.

I recommend you try to set the transport to just xhr-polling or jsonp-polling. It kinda defeats the cool factor of using node.js but it might help you reach a resolution to your problem.



回答3:

If your socket.io web application is hosted in an IIS virtual directory, socket.io configuration must be modified compared to a self-hosted case. Please see http://tomasz.janczuk.org/2013/01/hosting-socketio-websocket-apps-in-iis.html for details.

Also, as of version 0.2.x, iisnode does support WebSockets on Windows 8 and Windows Server 2012 with IIS 8. Check out http://tomasz.janczuk.org/2012/11/how-to-use-websockets-with-nodejs-apps.html for details.