簡體   English   中英

如何僅使用Pusher向個人觸發事件?

[英]How do I trigger an event to an individual only using Pusher?

我使用PHP和Pusher API編寫了聊天程序。 現在,我正在嘗試向單個用戶觸發事件,但是我不知道如何觸發事件。 每個用戶都由會話(用戶名)標識。 這就是我觸發消息的方式:

$pusher->trigger(
        'presence-nettuts', //the channel
        'new_message', //the event
        array('message' => $message) //the data to send
);

//echo the success array for the ajax call
echo json_encode(array(
        'message' => $message,
        'success' => true
));
exit();

我只想將事件定向到在線用戶,該在線用戶由使用會話的用戶名標識。

你能指導我怎么做嗎?

謝謝。

您的頻道名稱應對該用戶唯一。 因此,一種可能性是使用唯一的名稱(例如用戶名)創建頻道:

$pusher->trigger(
 'presence-nettuts-'.$username, //the channel
 'new_message', //the event
 array('message' => $message) //the data to send
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM