使用带有socket.io Backbone.js的 [英] Using backbone.js with socket.io

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

问题描述

我试图进入Node.js的世界,想建立一个简单但完整的测试应用程序挂接使用Redis的作为卖场与socket.io的Node.js和Backbone.js的。我发现一对夫妇的教程和几个例子也是如此。不知怎的,我只是感到困惑的是整个架构我一定要使用。通常你定义你的server.js与前preSS内的所有路由。所以,你得到在服务器端的路由的完全控制。现在,衔接必须重新定义路由骨干?这似乎是一个模型方面,但对我来说似乎是双重的工作,我不喜欢莫名其妙。
所以,我才刚刚困惑,工作的事情完全不同?也许有人有一个链接到它变得更加清晰了良好的教程或例子。

I'm trying to get into the node.js world and would like to build a simple but complete testing app to hook up node.js with socket.io and backbone.js using redis as a store. I found a couple of tutorials and a few examples as well. Somehow I just get confused about the whole architecture I have to use. Usually you'd define all your routes within your server.js with express. So you get a full control of your routes on the server side. Now linking up backbone you have to define routes again? This seems to be a model aspect but for me it seems like double-work which I somehow dislike. So did I just got confused and things work completely differently? Maybe someone has a link to a good tutorial or example where it's made more clear.

推荐答案

我使用前端骨干型号类似的东西。

I'm using something similar for front-end Backbone Model

class Model extends Backbone.Model

    idAttribute: '_id'

    _sync: (method, model, options) =>
        options.data ?= {}
        @socket.emit method, @name(), model.toJSON(), options.data, (err, data) => 
            if err then console.error "error in sync with #{method} #{@.name()} with server (#{err})" else options.success(data)

    sync: (method, model, options) => 
        if @socket.connected() is no 
            @socket.once 'connect', => @_sync method, model, options
        else 
            @_sync method, model, options

    name: => 
        if @collection and @collection.name then return @collection.name else throw new Error "Socket model has no name (#{@.collection})"

    initialize: ->
        @socket = require('socket')

module.exports = Model

这是我的基本骨干集合

And this is my basic Backbone Collection

class Collection extends Backbone.Collection

    model: require('class/socket/model')

    _sync: (method, collection, options) =>
        @socket.emit method, @.name, collection.toJSON(), options.data, (err, data) => 
            if err then console.error "error in sync with #{method} #{@.name} with server (#{err})" else options.success(data)

    sync: (method, collection, options) => 
        if @socket.connected() is no 
            @socket.once 'connect', => @_sync method, collection, options
        else 
            @_sync method, collection, options

    garbage: false

    register: =>
        @socket.emit 'register', @name

    deregister: =>
        @socket.emit 'deregister', @name
        @garbage = true

    initialize: (options) ->
        @name = options.name if options and options.name
        if !@name then throw new Error 'Socket collection has no name'
        @socket = require('socket')

        # Registrating socket for connection
        @socket.on 'connect', => @register() if @garbage is off
        if @socket.connected() is yes then @register()

        @fetch()        

        # Registration for socket events for this collection
        @socket.on @name, (method, model) =>

            if method is 'reset'
                @reset(model)

            if method is 'delete'
                m = @get model._id 
                m.trigger 'destroy', m, m.collection

            if method is 'update'
                m = @get model._id
                m.set(model)

            if method is 'create'
                @add(model)

            console.log "SOCKET: " + method + " triggered for collection " + @name

module.exports = Collection;

这是CoffeeScript的,如果你不介意的话。

It's CoffeeScript if you don't mind.

这是我见过使用注册/注销对集合所有教程。

All tutorials that I have seen use register / deregister for collections.

重要的东西是要找到一种方法来介绍认证与这些收藏品和模型的后端。这并不难,但它是棘手的。

The important stuff is to find a way to introduce authentication to the backend with those collections and models. It's not that hard, but it's tricky.

另外要小心为同一用户管理两个套接字连接。它需要插座更新发送给同一用户,但可以对其他连接

Also be careful for managing two socket connections by the same user. It needs to send socket update to the same user but to the other connection.

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

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