在我的项目中,后台发送了很多发布到不同渠道的消息。
我可以从浏览器控制台看到到达的消息具有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;
}所以我的问题是,前端开发人员如何找出到达的消息发布到哪个渠道,以便能够正确处理该消息?
发布于 2016-03-08 06:37:48
有点晚了,但以防你不能解决这个问题:
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查看列表中是否存在您感兴趣的频道。
https://stackoverflow.com/questions/34897685
复制相似问题