2

我正在尝试将推文流式传输到客户端并且一切正常,只要只有一个连接。如果我在浏览器的一个选项卡中流式传输推文并在新选项卡中打开页面,第一个选项卡将停止接收新推文,只有第二个选项卡会接收它们。

服务器代码。

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? socket.emit('twitter', data) : ''
    });
 });

客户端代码:

var socket = io.connect()

socket.on('twitter', function (data) {

    //Do really awesome things.

});

我究竟做错了什么?它必须很简单。我无法想象它很难支持多个连接。

干杯

4

1 回答 1

1

尝试更改socket.emitsockets.emit

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? sockets.emit('twitter', data) : ''
    });
 });

或者如果这不起作用,添加socket.broadcast.emit如下socket.emit所示:

io.sockets.on('connection', function (socket) {

  var twitter = require('ntwitter'),
  util = require('util'),
  twit = new twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
  });

 twit.stream('statuses/filter', {'locations':'-124.46, 24.31, -66.57, 49.23'}, 
    function(stream) {
       stream.on('data', function (data) {
         data.geo  ? socket.emit('twitter', data) : ''
         data.geo  ? socket.broadcast.emit('twitter', data) : ''
    });
 });
于 2012-11-12T01:44:40.430 回答