很抱歉再次发布这个问题,但是大部分相关的帖子都没有回答我的问题。我在使用与socket.io的多个连接时遇到了问题--我不能让"socket.socket.connect“方法工作,但是我从第一个连接中得到反馈。
这是我的结构:
var iosocket = null;
var firstconnection = true;
var ip = "http://xxx.xxx.xxx"
var ipPort = 8081
function callSocket() {
iosocket = null;
iosocket = io.connect(ip,{port:ipPort,rememberTransport:true, timeout:1500});
if (firstconnection) {
firstconnection= false;
iosocket = io.connect(ip,{port:ipPort,rememberTransport:true, timeout:1500});
iosocket.on('connect', function () {console.log("hello socket");});
iosocket.on('message', function(message) {});//end of message io.socket
iosocket.on('disconnect', function () {console.log("disconnected");});
} else {
if (iosocket.connected === true) {
console.log("heyhey still connected");
iosocket.disconnect();
}
iosocket.socket.connect(ip,{port:ipPort,rememberTransport:true,timeout:1500});
}
};它从第二个连接中得不到任何反馈
发布于 2014-11-26 14:31:03
我只是通过添加IE8 bug来解决这个问题
<!DOCTYPE html>在html的顶部
发布于 2014-08-08 12:42:43
我想我知道为什么这行不通了。对于服务器端代码,这似乎不适合socket.io。connect方法用于客户端而不是服务器。我想你是想让服务器监听一个端口。在这种情况下,您应该这样做:
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
io.on('connection', function (client) {
client.on('someEvent', function(someVariables){
//Do something with someVariables when the client emits 'someEvent'
io.emit('anEventToClients', someData);
});
client.on('anotherEvent', function(someMoreVariables){
//Do more things with someMoreVariables when the client emits 'anotherEvent'
io.emit('anotherEventToClients', someMoreData);
});
});
server.listen(8000);https://stackoverflow.com/questions/25163689
复制相似问题