1

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

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

4

1 回答 1

1

是的,官方的“存在”功能已经完成并在测试中。但是,您已经可以通过侦听来模仿它的功能,如本教程中的这行代码所示。

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

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 );
        }
    }

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

于 2016-10-08T11:14:16.547 回答