【问题标题】:Php know if a user has read the message sent and also get all unread messagesPhp 知道用户是否已阅读发送的消息并获取所有未读消息
【发布时间】:2017-10-15 08:07:41
【问题描述】:

我与网络套接字进行实时聊天并将消息存储在 MySQL 数据库中

以下是我的数据库结构

tbl_messages

id
message
sent_by
sent_on

关系的另一个表,在用户删除消息以仅删除他的关系时很有用

tbl_message_users

message_id
user_id
read // 0 or 1 

上面的read 默认为 0(未读),用于向其发送消息的用户。

现在,在用户单击发送按钮后,在我的网络套接字中,nodejs 中触发了一个发送消息事件,该事件执行一个函数以再次检索消息以进行更新。

不确定如何实现未读消息。假设用户离线,我希望他收到关于未读消息的通知,这些消息是带有read=0 的消息。

假设还有一个用户已经登录但还没有打开聊天框也变成了一条未读消息。

我该怎么做?

【问题讨论】:

    标签: javascript php mysql


    【解决方案1】:

    对不起,男孩,但我不明白你在哪里找到问题......这真的很简单


    案例1:离线用户通知(服务器端)

    当用户登录聊天服务时,您需要在“tbl_messages_users”中使用参数“read = 0”进行查询。然后你必须对数据库的输出进行风格化

    /** I'm using PHP syntax because I'm not good with NodeJS **/
    
    $query = new Query(...);
    $results = $query->getResults();
    if(count($results) > 0) {
        foreach($results as $result) {
            /** .... STYLISH THIS (Ex. in HTML) .... **/
        }
    }
    

    trick怎么用?服务器为用户生成页面时(服务器端)


    案例 2: 在线用户(客户端)

    /** I'm using jQuery syntax **/
    
    /**
    * %HTTP_METHOD%                   :    GET or POST ?
    * %SERVER_SIDE_URL%               :    Where is the action?
    * %DATA_TO_SERVER_SIDE_ACTION%    :    Example ClientID
    *
    * We use JSON Data Object because is really simple to parse in jQuery
    **/
    $.ajax({
        type: "%HTTP_METHOD%",
        url: "%SERVER_SIDE_URL%",
        data: "%DATA_TO_SERVER_SIDE_ACTION%"
    }).done(function(json) {
        /** indexOf prevent invalid HTML characters **/
        json = json.substring(json.indexOf("{")); 
        json = $.parseJSON(json);
    
        /** Create Your JSON Data Structure, iterate It and generate HTML to append in notification panel **/
    
    }).fail(function(httpResponse) {
       /** 
       * Stylish an HTML exception 
       * httpResponse    :    Contains Server HTTP response data (Ex. response code like 200, ..., 500) 
       **/
    });
    

    如何调用 Ajax 请求?

    setTimeout(function() {
       myAjaxRequest( ...params ):
    }, %EVERY_X_MILLISECONDS%);
    

    或使用触发器

    $(document).on("%MY_TRIGGER%", myCallbackFunction);
    
    function myCallbackFunction(e) {
        myAjaxRequest( ...params ):
    }
    

    如何使用技巧?当用户调用服务器 API(客户端)时


    将案例 1 与案例 2 结合

    在%SERVER_SIDE_URL%(案例2)中可以放一个返回JSON数据对象的URL

    /** I'm using PHP syntax because I'm not good with NodeJS **/
    
    $query = new Query(...);
    $results = $query->getResults();
    $RESULT = array();
    if(count($results) > 0) {
        $RESULT['status'] = 'OK';
        $RESULT['unreadMessages'] = array();
        foreach($results as $result) {
            $RESULT['unreadMessages'][] = array(
                'message' => $result->message,
                'date' => $result->messageDate,
                /** ... etc **/
            );
        }
    } else { 
        $RESULT = array(
            'status' => 'KO',
            'error' => 'No unread messages for user %USER_ID%',
        );
    }
    
    echo json_encode($RESULT);
    exit; /** PREVENT SCRIPT TO REMAIN ALIVE IN BACKGROUND **/
    

    之后

    通过 Ajax 请求调用服务器页面(见案例 2)


    结论

    当用户登录聊天系统或用户也进入聊天系统时,您可以使用案例 2。只需在需要时启动 jQuery 触发器! =)

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 2013-10-04
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多