2012-12-16 14 views
4

我使用Socket.io使用Twitter的Streaming API(我的实现基于this tutorial或多或少)向我的用户发送实时推文。如何使用Socket.io禁用多路复用

问题是,每次由Socket.io触发连接事件时,新连接的客户端都会导致连接到服务器的每个其他客户端停止更新。虽然经历所有我尝试过的黑客手段需要很长时间,但我会说我玩得够多,我相信这个问题是由于Socket.io将来自多个客户端(默认启用)的连接复用为一个性能提升,允许多个客户端或连接共享相同的底层套接字。简而言之,我认为情况就是这样,因为我认为如果不是用于连接复用,新连接不会以这种方式影响较旧的连接。换句话说,如果每次客户端连接它时都会创建一个与其自己的基础(TCP)套接字的新独立连接,这将不可能发生,因为一个连接对另一个连接一无所知,因此不会影响任何其他连接客户的状态与目前正在发生的一样。这也使我相信,简单地禁用多路复用功能将是解决此问题的最简单方法,因为我不关心扩展,因为Node.js已经处理了我可能需要处理的所有并发性。

我已经通过Socket.io的documentation,但无法看到“解复用”连接的能力是通过API公开的,所以如果有人知道如何做到这一点,我会创建欣赏你的回应。

我在下面的代码非常标准和简单。但要清楚的是,问题是每当新的客户端连接到Socket.io时,其他客户端都停止接收新的推文,并且更新不再被推送到较旧的客户端,除非我刷新浏览器,在这种情况下,新刷新的客户端将开始再次更新并接收新的推文,但其他仍然连接的客户端将停止更新。

服务器端代码:

// Code also uses ntwitter (https://github.com/AvianFlu/ntwitter) as an abstraction over Twitter's Streaming API 
io.sockets.on('connection', function (socket) { 
    tweet.stream('statuses/filter', { track : 'new orleans' }, function (stream) { 
    stream.on('data', function (data) { 
     // The following lines simply pre-process data sent from Twitter so junk isn't 
     // unnecessarily sent to the client. 
     if (data.user) { 
     tweets = { 
     text : data.text, 
     image : data.user.profile_image_url, 
     user : data.user.name 
     }; 
     var t = JSON.stringify(tweets); 
     console.log(t); 
     socket.send(t); 
     } 
    }); 
    }); 
}); 

客户端代码

// Notice the option that I passed in as the second argument. This supposedly forces every 
// new client to create a new connection with the server but it either doesn't work or I'm 
// implementing it incorrectly. It is the very last configuration option listed in the 
// documentation linked to above. 
var socket = io.connect('http://' + location.host, {'force new connection' : true }); 
socket.on('message', function (tweet) { 
    var t = JSON.parse(tweet); 
    if (t.image) { 
    $('.hero-unit').prepend('<div class="media"><a class="pull-left" href="#"><img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="' + t.image + '"></a><div class="media-body"><h4 class="media-heading">' + t.user + '</h4>' + t.text + '</div></div>'); 
    } 
}); 

如果我想的这个错误,或如果有什么错我的代码我肯定是开放的任何建议。我也很乐意回复任何额外的细节。先谢谢了!

+2

socket.io的复用只能应用于同一客户端和服务器之间的连接。使用相同的套接字同时与不同的通信伙伴进行通信在技术上是不可能的(除非您使用多播,这在TCP上不起作用)。 – Philipp

回答

0

我会尝试这样的事情

Serverside集团:

io.sockets.on('connection', function (socket) { 
    //Other Connectiony goodness here. 
    }); 
}); 

tweet.stream('statuses/filter', { track : 'new orleans' }, function (stream) { 
    stream.on('data', function (data) { 
     // The following lines simply pre-process data sent from Twitter so junk isn't 
     // unnecessarily sent to the client. 
     if (data.user) { 
     tweets = { 
     text : data.text, 
     image : data.user.profile_image_url, 
     user : data.user.name 
     }; 
     var t = JSON.stringify(tweets); 
     console.log(t); 
     io.sockets.emit("tweet", t); 
     } 
}); 

客户端:

var socket = io.connect('http://' + location.host, {'force new connection' : true }); 
socket.on('tweet', function (tweet) { 
    var t = JSON.parse(tweet); 
    if (t.image) { 
    $('.hero-unit').prepend('<div class="media"><a class="pull-left" href="#"><img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="' + t.image + '"></a><div class="media-body"><h4 class="media-heading">' + t.user + '</h4>' + t.text + '</div></div>'); 
    } 
}); 

主要有来自Twitter的数据流的插座之外,再上一个新的鸣叫emit给所有连接的消息。