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')++ );
我希望这会有所帮助!