-1

全部:

正如标题所问,我对 Express 和 Deepstream 还很陌生,我想知道如何让它们一起工作(或者他们必须分开工作?)?

在 Deepstream 指南部分(https://deepstream.io/tutorials/integrations/)它只提供了如何连接前端和持久层,但没有太多关于如何与其他 REST API 服务合作(只有一小部分:https://deepstream.io/tutorials/integrations/other-http/,但没有太多用例)

任何教程/示例(简单的案例,如授权聊天和朋友聊天列表)?

谢谢

4

1 回答 1

3

Deepstream 目前可以通过一个 http 服务器来初始化它的 websocket 服务器。然而,这种行为在不久的将来会被弃用,因为我们希望通过将 deepstream.io 更多地移动到 c++ 空间来进一步优化 deepstream.io,这意味着 http 服务器将能够处理更多的并发连接,但代价是不可配置.

在大多数情况下,您可以使用 deepstream 客户端构建整个应用程序。数据同步、事件和 RPC 的组合意味着只要您不考虑执行诸如二进制上传/传输之类的操作,您就可以从选择完成任务的最佳工具中受益。

例如,当需要经典的请求/响应原子操作时,您可以使用 RPC:

  ds.rpc.provide( 'add-numbers', function( data, response ) {
    var result = data.reduce(function(previousValue, currentValue) {
      return previousValue + currentValue;
    })
    response.send( result )
  } )

  ds.rpc.make( 'add-numbers', [ 1, 3 ], function( err, data ) {
     console.log( 'result: ', data )
  } ) 

这类似于表达:

  app.post( '/add-two', function( req, res ) {
    var result = req.body.data.reduce(function(previousValue, currentValue) {
      return previousValue + currentValue;
    })
    res.send( result )
  } )

除了 deepstream 还会为您管理服务发现和负载平衡等概念,甚至允许客户端在需要时成为端点。

或使用事件:

ds.event.emit( 'score-changed', score++ )

ds.event.subscribe( 'score-changed', function( newScore ) {
    console.log( `The score changed on: ${newScore}` )
} )

或为您的应用程序数据建模并使用记录在所有客户端之间进行同步:

var record = ds.record.getRecord( 'playerA' )
record.subscribe( 'score', function( newScore ) {
  console.log( `Player A score has changed to: ${newScore}` );
} );    
record.set( 'score', record.get('score')++ );

我希望这会有所帮助!

于 2016-10-12T12:31:58.197 回答