在Backbone.js的应用程序中使用的WebSockets没有Socket.io [英] Using websockets in a Backbone.js app without Socket.io

查看:119
本文介绍了在Backbone.js的应用程序中使用的WebSockets没有Socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骨干,我试图建立一个主干应用程序,图表和地图数据的实时性。我实现了一个如下的WebSocket <一个href=\"https://github.com/andrewvc/d3-backbone-$p$psentation/blob/master/04_websockets_bb.html\">this例如code。问题是,我想用一个更广泛的数据集比例如:code,如果我理解code,它只是创造一个模型 - 点的单一阵列。我想要的模型的集合,其中每个模型都有纬度,经度和金额(只是一个数值)。

I'm new to Backbone and I'm trying to build a Backbone app that graphs and maps data in real-time. I implemented a websocket following this example code. The problem is, I would like to use a more extensive data set than the example code, and if I understand the code, it is just creating one model -- a single array of points. I want a collection of models in which each model has latitude, longitude, and amount (just a numerical value).

我如何实现的WebSocket这样,当我的后台发送一些JSON,我的应用程序创建一个具有这些属性的新模式?我读过的关于这一点,我需要重写Backbone.sync和实施一个事件聚合博客,但唯一的例子我已经看到了这个使用socket.io的。 Socket.io是不是因为语言/框架,我使用的是后端的选项。此外,最终我会切换出后端还没有被socket.io支持的另一种语言,所以我想找到实现对前端WebSocket的一个更普遍的方式,不涉及就像一个图书馆socket.io。

How do I implement a websocket such that when my backend sends some JSON, my app creates a new model with those attributes? I've read on blogs about this that I need to override Backbone.sync and implement an event aggregator, but the only examples I've seen of this use socket.io. Socket.io is not an option because of the language/framework I'm using on backend. Moreover, eventually I'll be switching out the backend to another language that also isn't supported by socket.io, so I'd like to find a more general way to implement the websocket on the frontend that does not involve a library like socket.io.

推荐答案

我已经找到了解决我自己的问题的作品。同样,我是新来的骨干,所以我不知道这是最好的方式 - 会感兴趣反馈此解决方案是否遵循最佳实践。在code基于这个例子安德鲁Cholakian。我保存了一些打印语句在那些有用的,当你运行code。

I have found a solution to my own question that works. Again, I'm new to Backbone, so I'm not sure if this is the best way -- would be interested in feedback on whether this solution is following best practices. The code is based on this example by Andrew Cholakian. I kept some print statements in that are helpful when you run the code.

在code假设你的后端的形式发送JSON数据
{数据:{纬度:纬度,长:经度,AMT:量}}

The code assumes your backend is sending JSON data in the form of {data: "{"lat": latitude, "long": longitude, "amt": amount}"}

// this function opens the websocket and will trigger add_point when
// a new message is received
Stream = function () {
    _.extend(this, Backbone.Events);
    var self = this;

    self.socket = new WebSocket("ws://" + document.domain + ":5000/websocket");
    console.log("Using a standard websocket");

    self.socket.onopen = function(e) {
        self.trigger('open', e);
        console.log('socket opened');
    };

    self.socket.onerror = function(e) {
        self.trigger('error', e);
    };

    self.socket.onmessage = function(e) {
        self.trigger('message', e);
        self.trigger('data', e.data);
        self.trigger('add_point', JSON.parse(e.data));
    };

    self.socket.onclose = function(e) {
        self.trigger('close', e);
        console.log('socket closed');
    };
};  

DataPoint = Backbone.Model.extend({
    defaults: {
        lat: null,
        long: null,
        amt: null
        }
});

DataSet = Backbone.Collection.extend({
    model: DataPoint,
    initialize: function(options) {
        this.stream = options.stream;
        var self = this;
        this.stream.on("add_point", function(pt) {
            self.add( new DataPoint({
                lat: pt.lat,
                long: pt.long,
                amt: pt.amt
            }));
            console.log('updated collection');
            console.log(self.models);
        });
    }
});

MapView = Backbone.View.extend({
    initialize: function(options) {
        this.dataSet = options.dataSet;
    }
});

$(function() {
    var stream = new Stream();
    var dataSet = new DataSet({ stream: stream });
    var mapView = new MapView({ dataSet: dataSet });
});

这篇关于在Backbone.js的应用程序中使用的WebSockets没有Socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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