沼泽龙:如何确定消息发布到的频道

Swampdragon: how to determine a channel the message was published to?

本文关键字:频道 消息 何确定 沼泽      更新时间:2023-09-26

在我的项目中,后端会发送大量发布到不同频道的消息。
我可以从浏览器控制台看到到达的消息具有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检查列表中是否存在您感兴趣的频道。