1

错误:

尝试访问http://localhost/appDirectory/socket.io给出:
Cannot GET /appDirectory/socket.io

或者换一种说法,当尝试在页面上加载客户端文件时,我收到此错误:
GET http://localhost/appDirectory/socket.io/socket.io.js 404 (Not Found)

如果我将客户端文件作为静态内容加载,则连接行会产生此错误:
GET http://localhost/appDirectory/socket.io/1/?t=1365535131937 404 (Not Found)

服务器代码:

var express = require('express'),
namespace = require('express-namespace'),
routes = require('./routes'),
http = require('http'),
app = express(),
server = app.listen(process.env.PORT),
io = require('socket.io').listen(server);
var appDir = '/appDirectory';

app.configure(function(){
  app.set('env', process.env.NODE_ENV || 'development');
  app.set('/views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(app.router);
  app.use(appDir, require('stylus').middleware(__dirname + '/public'));
  app.use(appDir, express.static(path.join(__dirname, '/public')));
});

app.get(appDir + '/', routes.index);

io.sockets.on('connection', function (socket) {
  socket.emit('message', 'lol');
});

客户端代码:

<script src="/appDirectory/socket.io/socket.io.js"></script>
<script type="text/javascript">
  var socket = io.connect(appDir, { resource: appDir.substring(1) + '/socket.io' });
  socket.on('connect', function () {
    console.log('connected');
  });
</script>

让这个工作的网络配置文件是......

网络配置:

<configuration>
<system.webServer>
  <handlers>
    <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
  </handlers>
  <iisnode loggingEnabled="true" debuggingEnabled="true" debuggerPathSegment="debug" />
  <rewrite>
    <rules>
      <clear />
      <rule name="Debug" patternSyntax="Wildcard" stopProcessing="true">
        <match url="app.js/debug*" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
        <action type="None" />
      </rule>
      <rule name="app" patternSyntax="Wildcard">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
        <action type="Rewrite" url="app.js" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>

我的直觉是 express 路由干扰了 socket.io 路由,或者 socket.io 路由与虚拟目录不兼容。我确实看到一些关于 socket.io 命名空间的提及,我确实尝试了以下...

io.of(appDir).on('connection', function (socket) {
  socket.emit('message', 'lol');
});

但这似乎并没有解决问题。

我在客户端代码方面尝试了许多不同的方法,但我认为这不是根本问题,http://localhost/appDirectory/socket.io/socket.io.js也不会起作用。

该应用程序作为虚拟目录运行,并建立在:

- node.js 0.10.3 for windows
- iis 7
- iisnode 0.2.4
- express.js 3.0.0rc1  
- jade  
- socket.io  

模块“ express namespace ”似乎使事情正常进行。

我知道 iisnode 被“设计”为作为单独的站点运行,我也知道 express 不喜欢在虚拟目录中,但我很确定这是可能的。

有趣的更新!

换行:

io = require('socket.io').listen(server);

到:

io = require('socket.io').listen(server, { resource: appDir + '/socket.io' });

导致 iis 工作进程 w3wp.exe 崩溃...如此
处所示http://i.imgur.com/65RGia3.png?1

但是,该网址http://localhost/appDirectory/socket.io似乎可以正常工作。

修复此 FYI 的尝试失败:

  • 为 iisnode nodeProcessCountPerApplication="1" 添加 web.config 设置

可能的解决方案:

如果您遇到同样的问题,我发现这可能会对您有所帮助...
http://tomasz.janczuk.org/2013/01/hosting-socketio-websocket-apps-in-iis.html

4

2 回答 2

1

有点摸不着头脑,但如果您使用的是 Express 3.x,您的问题可能是您使用的是旧的 Express 2.x 语法来连接 Socket.io 和 Express。

有关新的集成方法,请参阅:https ://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x#socketio-compatibility。

于 2013-04-09T20:58:41.653 回答
1

看起来你在正确的轨道上。使用以下配置,它适用于我(但不使用 IIS):

// server
io = require('socket.io').listen(server, { resource : '/appDirectory/socket.io' });

// client
<script src="/appDirectory/socket.io/socket.io.js"></script>
...
var socket = io.connect('', { resource: 'appDirectory/socket.io' });

我想知道为什么您的 IIS 进程崩溃了,您可以将图像发布到其他地方(Dropbox,免费图像托管)吗?

于 2013-04-10T07:35:45.973 回答