phpwebsocket 一次只有一个连接
我正在使用 phpwebsocket。有没有办法一次只有一个用户连接?
如果第二个用户尝试连接,他们应该自动断开连接,如果第一个用户在给定的时间内空闲,他应该被断开连接,以便为新用户腾出空间。
这可能吗?如果可以,有人知道怎么做吗?
I am playing with phpwebsocket. Is there a way to have only one user connected at a time?
If a second user tries to connect they should be automatically disconnected and if the first user is idle for a given amount of time he should be disconnected to allow space for a new user.
Is this possible - and if so, does anyone know how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,可以将用户数量限制为一名用户。您必须查看用户数并决定接受或不接受新连接。在代码中,它看起来像这样:
您可以进一步执行 if 语句来检查,如果 usercount 为 0,则您接受连接:
要断开静默用户的连接,每当用户向服务器发送消息时,我都会刷新用户的时间戳。现在唯一要做的就是检查用户时间戳和当前时间之间的差异是否高于您的断开连接时间。如果是这样,踢他:)
It's possible to limit usercount to one user, of course. You have to look at usercount and decide to accept or not new connections. In code it looks like this:
You can make a further if statement to check, if usercount is 0, so you accept connection:
To disconnect a silent user, i would refresh timestamp of user evertime the user sends a message to server. Now the only thing to do is to check, if diffence between users timestamp and current time is higher then your disconnect time. If so, kick him :)