错误:
尝试访问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