0

我正在制作一个 PHP 和 MySQL(和 CodeIgniter)视频/聊天程序,它1)显示当前在线用户,以及2)在主持人离开时强制所有用户断开连接。

但是 HTTP 是无状态的。实现上述功能的最佳方法是什么?

我应该为每个用户使用心跳 AJAX 请求来确定当前状态吗?我如何知道用户(例如版主)是否断开连接?

4

2 回答 2

1

This would require a close to real-time implementation. You could have a table that has all the rooms / chat sessions created. Then in your users table you could have a moderators column as well as a current room id column.

Rooms Table

Columns: ID, USER_ID, CREATED_AT, UPDATED_AT, NAME, PASSWORD, HAS_MODERATOR (bool) ....

When a moderator joins a room, you could update the "HAS_MODERATOR" field to true or 1. Then you could write a simple long-polling script that would simply make an ajax request to a php page or route and return a value of true or false. If it returns true, then a moderator is still in the room, otherwise you can close the room and force a redirect through javascript or something. The php script would simply check if has_moderator is true, AND you would go through the users table, retrieve all the moderators, and check what room they are in. Match the current room the moderator is in with the room your checking for. This will ensure that a moderator is in the room.

This might help you out: Notify Ajax/Javascript that background PHP has completed

于 2012-08-01T02:32:48.443 回答
1

我认为您可以在不使用 HTTP 的状态逻辑的情况下实现它。您将需要在数据库中存储当前的对话状态。每个用户请求都将有一个经过身份验证的用户名 cookie 和一个会话 ID,用于从后端数据库中提取会话。此外,将有一个变量来检查主持人是否存在(heartbit 样式检查)。当主持人离开时,只需更改会话状态即可让所有其他人退出。如果您需要我建议的任何步骤的任何详细信息,请告诉我。

于 2012-08-01T06:14:48.623 回答