[英]Swampdragon: how to determine a channel the message was published to?
在我的項目后端,許多消息已發布到不同的渠道。
我可以從瀏覽器控制台中看到到達的消息具有channel
屬性。 但是問題是傳遞給swampdragon.onChannelMessage
的回調沒有獲取該頻道信息。 相反,它獲得了奇怪的頻道列表。
因此,當消息到達(在瀏覽器中)時,我無法確定消息發布到的渠道,因此無法正確處理。
我找到了剝離頻道信息的代碼https://github.com/jonashagstedt/swampdragon/blob/master/swampdragon/static/swampdragon/js/dist/swampdragon.js#L261
if ('channel' in e.data) {
var channel = swampDragon.channels[e.data.channel];
delete(e.data['channel']);
swampDragon.settings.onchannelmessage(channel, e.data);
return;
}
因此,我的問題是前端開發人員如何才能弄清楚消息到達的渠道是為了正確處理消息而發布的?
有點晚了,但是以防萬一您無法解決這個問題:
swampdragon.open(function() {
swampdragon.subscribe('notification', 'notification', null, function (context, data) {
// Successfully subscribed to the notification channel
}, function () {
console.error('Error', arguments);
});
});
swampdragon.onChannelMessage(function(channels, message) {
if (channels.indexOf('notification') > -1) {
// Message sent on the notification channel
}
});
在onChannelMessage
, channels
參數是傳入消息發送到的信道數組。 您可以使用indexOf
檢查列表中是否存在您感興趣的頻道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.