【问题标题】:Deepstream.io - Server callback when client disconnects?Deepstream.io - 客户端断开连接时的服务器回调?
【发布时间】:2016-10-08 06:06:26
【问题描述】:

我正在使用授权服务器模型使用 Deepstream 构建多人游戏。我的服务器只是另一个 Node.JS 客户端。我的服务器有什么办法可以查明是否有任何已连接的客户端断开或关闭了它们的连接?是否有暴露的事件或回调?

我可以建立一个心跳系统,但我想知道这是否可以避免。

【问题讨论】:

  • 您的游戏是否使用任何 websockets 库(Socket.io)?

标签: node.js client-server game-engine multiplayer deepstream.io


【解决方案1】:

是的,官方"presence" feature 已经完成功能并处于测试阶段。但是,您已经可以通过收听来模仿它的功能,如this tutorialthis line of code 所示。

一般来说,所有客户端都会订阅特定于他们的状态事件,例如

ds.event.subscribe( 'status/' + name );

服务器现在将监听这些事件的订阅并推断在线状态:

ds.event.listen( 'status/.*', this._playerOnlineStatusChanged.bind( this ) );

_playerOnlineStatusChanged( match, isSubscribed ) {
        // Extract the player name from the status event
        var name = match.replace( 'status/', '' );

        if( isSubscribed ) {
            this.addPlayer( name );
        } else {
            this.removePlayer( name );
        }
    }

这将跟踪任何断开连接,无论是有意还是无意

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 1970-01-01
    • 2020-07-21
    • 2018-02-09
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2021-12-28
    相关资源
    最近更新 更多