簡體   English   中英

使用Sockets.io將流數據的子集發送到Node客戶端

[英]Sending subsets of streaming data to Node clients using Sockets.io

我有一個節點服務器,它使用Sockets.io和ntwitter在客戶端連接時向客戶端發送流式推文。 目前,所有(來自不同用戶)的推文都發送給每個客戶端。 但是每個客戶端僅需要某些特定的tweet子集,我希望服務器僅發送該子集(或類別)。

認為在Sockets中讓每個類別都像一個房間一樣有效,但是我還不太清楚如何使我的代碼適應使用它們。 或者,鑒於客戶端之間沒有通信,也許這不是最佳解決方案?

當前代碼的相關的,簡化的位...

客戶:

var socket = io.connect(window.location.hostname);

socket.on('messages', function(messages_packet) {
    $.each(messages_packet, function(idx, tweet) {
        if (tweet_is_in_this_clients_category(tweet)) {
            display_messages(messages_packet);
        };
    }
});

服務器:

// [A function which, on start-up, fetches existing tweets and caches them.]

// Send cached messages when a client connects.
sockets.sockets.on('connection', function(socket) {
    socket.emit('messages', cached_messages);
});

// Fetch tweets from the stream, and send new ones to clients.
twitter.stream('statuses/filter', {follow: [12345, 345678, etc]}, function(stream) {
    stream.on('data', function(tweet) {
        add_tweet_to_cache(tweet);
        sockets.sockets.emit('messages', [tweet]);
    }
});

因此, sockets.emit()部分當前正在將每條推文發送給所有客戶端。 然后,客戶決定是否顯示該tweet(如果屬於該客戶的類別)。 如果服務器僅向正確類別的客戶端發送推文,顯然會更有效率。 給定服務器已經知道哪些推文屬於哪些類別,我如何只將它們發送給那些類別,而不是每個客戶端?

經過一番反復嘗試后,我似乎可以正常使用了。 不知道這是最好還是正確的方法,但是...上面的代碼已經過調整,因此現在類似於下面所示。 客戶端代碼有一個附加內容,服務器代碼有兩個附加內容。

客戶:

var socket = io.connect(window.location.hostname);
// NEW CLIENT PART:
socket.on('connect', function(){
    // room_name was created based on the URL this page was requested at:
    socket.emit('subscribe', room_name);
};

socket.on('messages', function(messages_packet) {
    $.each(messages_packet, function(idx, tweet) {
        if (tweet_is_in_this_clients_category(tweet)) {
            display_messages(messages_packet);
        };
    }
});

服務器:

// [A function which, on start-up, fetches existing tweets and caches them.]

// Send cached messages when a client connects.
sockets.sockets.on('connection', function(socket) {
    // NEW SERVER PART 1:
    socket.on('subscribe', function(room_name) {
        socket.join(room_name);
        socket.emit('messages', cached_messages_for_this_room);
    });
});

// Fetch tweets from the stream, and send new ones to clients.
twitter.stream('statuses/filter', {follow: [12345, 345678, etc]}, function(stream) {
    stream.on('data', function(tweet) {
        add_tweet_to_cache(tweet);
        // NEW SERVER PART 2:
        // Gets the array of room_names this twitter account is associated with:
        var rooms = get_rooms_for_twitter_account(tweet.user.screen_name);
        rooms.forEach(
            function(room_name) {
                sockets.sockets.in(room_name).emit('messages', [tweet]);
            };
        );
    };
});

因此,當客戶端連接時,它將發送“訂閱”消息以及要加入的會議室的名稱。

當服務器接收到“訂閱”事件時,它僅發送與該會議室關聯的現有推文(獲取該推文列表的邏輯在其他地方完成;與該特定問題無關)。

並且,每當服務器接收到一條新的tweet時,它就會找到與該tweet關聯的房間,然后將該tweet發出給每個客戶端。

這似乎可行……確實指出了沒有任何意義或可以做得更好的任何事情!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM