2012-12-16 15 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給所有連接的消息。