1

我正在尝试进入 node.js 世界,并希望构建一个简单但完整的测试应用程序来连接 node.js 与 socket.io 和使用 redis 作为存储的主干.js。我还找到了一些教程和一些示例。不知何故,我只是对我必须使用的整个架构感到困惑。通常你会在你的 server.js 中用 express 定义你的所有路由。因此,您可以完全控制服务器端的路线。现在连接主干,您必须再次定义路由?这似乎是一个模型方面,但对我来说,这似乎是我不喜欢的双重工作。那么我是否只是感到困惑并且事情的运作方式完全不同?也许有人有一个链接到一个很好的教程或例子,它变得更清楚了。

4

2 回答 2

1

我正在为前端骨干模型使用类似的东西

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

这是我的基本骨干收藏

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。

我见过的所有教程都使用注册/注销集合。

重要的是找到一种方法,通过这些集合和模型将身份验证引入后端。这并不难,但很棘手。

还要小心管理同一用户的两个套接字连接。它需要将套接字更新发送给同一个用户,但发送给另一个连接。

于 2013-01-07T16:19:28.553 回答
1

现在连接主干,您必须再次定义路由?

取决于你所说的路线。

您需要告诉主干在哪里可以找到服务器资源,因此模型告诉它在哪里获取它是有意义的(模型中的 url 参数)。

主干中的路由类与服务器上的路由无关。它只是一种更改应用程序状态或页面内显示视图的方法。

例如,在 LOB 应用程序中,您有一个列表视图和一个详细视图。

主干允许您通过路由器在视图之间切换而无需刷新页面

列表的 url 可以是http://app.com/#list,详细视图的 url 可以是http://app.com/#detail/:id其中 id 是产品 ID。您只需单击定义为的链接即可在视图之间切换而无需刷新页面

<a href="#detail/1">product 1</a> 

并返回列表视图

<a href="#list">product list</a>

然后你可以有一个显示产品表单的视图

<a href="#newproduct">Create a new product</a>

等等。因此,您不必在视图中设置事件侦听器以在不应该相互了解的视图之间切换,并且无需向服务器请求任何内容且无需刷新页面。这是一种构建应用程序的便捷方式。

于 2013-01-07T11:06:38.907 回答