【问题标题】:Adding custom fields to a subscription response using the faye ruby server使用 faye ruby​​ 服务器将自定义字段添加到订阅响应
【发布时间】:2014-07-21 20:33:38
【问题描述】:

我正在尝试使用 faye(分发中包含的聊天示例)编写概念验证聊天应用程序。

在我的概念验证中,我想在客户订阅频道时发送聊天频道的完整历史记录。我目前的想法是使用订阅响应消息中的自定义字段来实现这一点。

在检查bayeux protocol definition 之后,似乎订阅响应消息中允许使用“ext”字段。

但我无法使用服务器扩展向此 ext 字段添加任何内容。

class ServerLog
  def incoming(message, callback)
    puts " msg: #{message}"
    unless message['channel'] == '/meta/subscribe'
      return callback.call(message)
    end

    # the following line changes absolutely nothing
    message['ext'] = 'foo'

    callback.call(message)
  end
end

App.add_extension(ServerLog.new)

虽然设置ext字段不会导致服务器崩溃,但对订阅响应消息绝对没有影响。我什至使用 Wireshark 进行了检查(只是为了确保 js 客户端不会忽略某些字段)。

【问题讨论】:

    标签: ruby chat publish-subscribe faye bayeux


    【解决方案1】:

    我的错误是使用“传入”方法,而不是“传出”。

    class ServerLog
      def outgoing(message, callback)
        puts " out: #{message}#"
        unless message['channel'] == '/meta/subscribe'
          return callback.call(message)
        end
    
        if message['subscription'] == '/chat/specialchannel'
          message['ext'] ||= {}
          message['ext']['specialattribute'] = 'special value'
        end
    
        callback.call(message)
    
      end
    end
    
    App.add_extension(ServerLog.new)
    

    本示例将specialattribute添加到订阅响应消息的ext字段(如果频道为/chat/specialchannel)。

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 2021-07-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2011-03-10
      • 2018-01-27
      • 2016-10-12
      • 2013-09-18
      相关资源
      最近更新 更多