0

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

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

在检查了bayeux协议定义之后,订阅响应消息中似乎允许使用“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 客户端不会忽略某些字段)。

4

1 回答 1

0

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

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)

此示例将添加specialattributeext订阅响应消息的字段(如果频道是/chat/specialchannel)。

于 2014-07-22T15:58:11.170 回答