无法获得IISNode,socket.io和express一起工作
请阅读我的问题,甚至会收到一小段忠告。
我在Google Chrome中收到以下错误:
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1419089743449-2 404 (Not Found)
我的文件夹设置如下:
本地主机
亲
上市
socket.io/socket.io.js
cssfile.css
jsfile.js
app.js
node_模块
在我看来,客户端的握手请求是错误的,因为错误应该是localhost / pro / public / socket.io /等等等等。
我正在使用以下设置:
web.config:
<handlers> <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="DynamicContent"> <match url="/pro/" negate='True'/> <!--<conditions> <add input="pro" matchType="IsFile" negate="True"/> </conditions>--> <action type="Rewrite" url="app.js"/> </rule> <rule name="LogFile" patternSyntax="ECMAScript"> <match url="socket.io"/> <action type="Rewrite" url="app.js"/> </rule> </rules> </rewrite>
客户端js:
var socket = io.connect('http: //localhost', {resource: 'pro/public/socket.io' });
服务器端js(节点):
var http = require('http'); var express = require('express'); var app = express(); var port = process.env.PORT || 3002; var server = http.createServer(app).listen(port); io = require('socket.io').listen(server, { resource : '/pro/public/socket.io' });
我希望得到的HTML和静态文件也服务; 我只是不能得到socket.io工作。
解:
更改:
io = require('socket.io')。listen(server,{resource:'/pro/public/socket.io'});
至
io = require('socket.io')。listen(server,{path:'/pro/public/socket.io'});
这是对我有用,我希望它也适用于你! 🙂
Drizo,我认为你的404上的socket.io是混合的东西,但我不是一个巨大的专家在这里! 🙂
首先,你的连接string缺less端口,你需要这个。
其次,我并不是100%确定你的socket.io初始化是正确的,尽pipe我使用了命名空间,并没有听说过资源,我的看起来像这样:
var app = require('express')(); var http = require('http').Server(app); var config = require('./config')(); var server = http.listen(config.server.port, function () { console.log('listening on *:' + config.server.port); }); var socketIo = require('socket.io'); var io = socketIo(http); var namespaceIo = io.of('/namespace'); namespaceIo.on('connection', function (socket) { //configuration for events follows socket.on('someEvent', function (input) { ... } }
使用这些命令,你可以命名空间一个socket.io的实现,客户端可以连接类似的东西
function initSocket(__bool){ if(__bool == true){ io(serverAddress + '/namespace'); if ( !socket ) { socket = io.connect(serverAddress + '/namespace'); } else { socket.socket.connect(serverAddress + '/namespace'); // Yep, socket.socket ( 2 times ) } // now that we have the socket we can bind events to it bindSocketEvents(); }else{ socket.disconnect(); } }