我是node.js和socket.io中的新手 我正在开发示例socket.io项目,我希望将参数传递给连接事件和连接到socket.io服务器的cordova应用程序
它在本地主机上工作正常,但在服务器中出错 我应该添加它,在连接事件成功后,应该发布请求
我尝试了很多解决方案但是dosnt工作
无法加载资源:服务器响应状态为404(未找到)http://serverip/socket.io/socket.io.js
获取http://serverip:3000/socket.io/?username=0912101&EIO=3&transport=polling&t=Lg_3duf net :: ERR_CONNECTION_TIMED_OUT socket.io.js
服务器上的
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var rest = require('restler');
var serverpath = 'http://serverip';
var userconnectiondata =
{
username: '',
cred: '',
socketid: ''
}
var connectionlist = [];
server.listen(3000);
console.log("server running");
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.use(function (socket, next) {
var userconnectiondata =
{
username: socket.handshake.query.username,
cred: socket.handshake.query.cred,
socketid: socket.id
}
console.log('con %s', socket.id);
connectionlist.push(userconnectiondata);
var jsonData = {
username: userconnectiondata.username,
SocketID: userconnectiondata.socketid
};
rest.postJson(serverpath + '/Chat/UpdateSocketId', jsonData).on('complete', function (data, response) {
// handle response
});
next();
socket.on('disconnect', function (data) {
for (var i = 0; connectionlist.length; i++) {
if (connectionlist[i].socketid == socket.id) {
connectionlist.splice(i, 1);
break;
}
}
console.log('discon %s', socket.id);
});
});
在客户端
<script src="http://serverip/socket.io/socket.io.js"></script>
var temp = "username=0912101" ;
socket = io("http://serverip:3000", {
query: temp
});
我感谢任何帮助我的朋友