gpt4 book ai didi

node.js - Deepstream.io - 客户端断开连接时服务器回调?

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:57 33 4
gpt4 key购买 nike

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

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

最佳答案

是的,官方"presence" feature功能已经完成并正在测试中。但是,您已经可以通过监听来模仿其功能,如 this tutorial 所示。在this 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 );
}
}

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

关于node.js - Deepstream.io - 客户端断开连接时服务器回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39929249/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com