发出socket.io和sailsjs(Twitter API) [英] emit socket.io and sailsjs (Twitter API)

查看:37
本文介绍了发出socket.io和sailsjs(Twitter API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了,我正在控制台中打印所有这些,但我想将数据发送到视图,但我不知道如何使用 socketio 来做到这一点.

有什么想法吗?

var TwitterController = {'索引':函数(请求,资源){var twitter = require('ntwitter');var twit = 新推特({消费者密钥:'...',消费者秘密:'...',access_token_key: '...',access_token_secret: '...'});twit.stream('statuses/filter', { track: ['dublin', 'spain']} , function(stream) {stream.on('数据',函数(数据){console.log(data.user.screen_name + ':' + data.text);req.socket.emit('tweet', {用户:data.user.screen_name,文本:数据.文本});});});res.view();},};module.exports = TwitterController;

在视图中,我试图打印所有的 twits

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script src="/socket.io/socket.io.js"></script><脚本>var socket = io.connect('http://localhost:1337');jQuery(函数($){var twList = $('ul.tw');});socket.on('tweet', 函数(数据){twList.prepend('
  • ' + data.user + '
  • ');});

    解决方案

    如果您尝试 Sails v0.9,那么您将可以更轻松地使用这个开箱即用的工具,因为捆绑的示例处理了以下内容的样板连接逻辑你.

    关键是,不要使用 res.view(),而是使用 res.json(tweets) 提供推文.

    在本例中,您使用的是流,因此您可以利用 res.pipe(stream).

    然后你就可以在前端使用sails.io.js向/twitter发送socket.io请求,并利用结果获取乐趣和收益.

    <块引用>

    以下前端代码假设您使用的是 Sails v0.9.x 中捆绑的微型客户端 SDK (sails.io.js):

    socket.get('/twitter', function (tweets) {console.log('从服务器得到以下推文 :: ', tweets);}

    <块引用>

    记住 socket.get 只是 socket.emit 之上的一个方便的方法,它与 Sails 路由器一起开箱即用.如果您有不想通过 HTTP 访问的特定于 pubsub 的代码,您仍然可以执行所有您喜欢的自定义 socket.io 操作.

    sails.io.js 还支持 socket.postsocket.putsocket.delete,具有与类似的 jQuery AJAX 方法(例如 $.post)

    I got the twits, I'm printing all of them ok in console, but I would like send the data to the view and I don't know how can I do it using socketio.

    any idea?

    var TwitterController = {
    
    'index': function(req,res) {
    
    var twitter = require('ntwitter');
    
    var twit = new twitter({
      consumer_key: '...',
      consumer_secret: '...',
      access_token_key: '...',
      access_token_secret: '...'
    });
    
    twit.stream('statuses/filter', { track: ['dublin', 'spain']} , function(stream) {
      stream.on('data', function (data) {
        console.log(data.user.screen_name + ': ' + data.text);
    
        req.socket.emit('tweet', {
                        user: data.user.screen_name,
            text: data.text
        });
    
      });
    });
           res.view();
    },
    };
    module.exports = TwitterController;
    

    and in the view I'm trying to print all the twits

    <ul class="tw"></ul>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:1337');
    
    
      jQuery(function($){
        var twList = $('ul.tw');
      });
    
      socket.on('tweet', function (data) {
        twList.prepend('<li>' + data.user + '</li>');
      });
    </script>
    

    解决方案

    If you give Sails v0.9 a try, you'll have an easier time w/ this out of the box since the bundled example handles boilerplate connection logic for you.

    The key is, instead of using res.view(), serve tweets with res.json(tweets).

    In this example, you're using streams, so you can take advantage of res.pipe(stream).

    Then you can use sails.io.js on the front-end to send a socket.io request to /twitter, and use the results for fun and profit.

    The following front-end code assumes you're using the tiny client-side SDK (sails.io.js) bundled in Sails v0.9.x:

    socket.get('/twitter', function (tweets) {
      console.log('Got the following tweets from the server :: ', tweets);
    }
    

    Keep in mind socket.get is just a convenience method on top of socket.emit that works with the Sails router out of the box. You can still do all the custom socket.io things you like if you have pubsub-specific code that you don't want to make accessible via HTTP.

    sails.io.js also supports socket.post, socket.put, and socket.delete, with a similar API to the comparable jQuery AJAX methods (e.g. $.post)

    这篇关于发出socket.io和sailsjs(Twitter API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆