【发布时间】:2015-05-15 17:04:58
【问题描述】:
我正在使用 websocket-rails gem 将 websocket 连接到我的 rails 应用程序 - 它在我的计算机上完美运行 - 但在我的生产机器上它无法运行。
当我连接时 - 它写入 websocket_rails 日志 - 我已连接 - 但是当我尝试向连接发送数据时 - 它什么也不做。
我添加了一些日志来帮助我找到问题,似乎 WebsocketRails.users 是空的 - 意味着没有连接 - 即使有。
但是,当我真正断开 websocket 连接时,它会记录断开连接 - 并查看代码,它会在记录断开连接之前检查它是否已连接 - 所以我发现它已连接。
这是触发连接事件的代码
class Route < ActiveRecord::Base
acts_as :user
has_many :students
after_update :notify_location_changed, if: Proc.new { |route| route.loc_latitude_changed? || route.loc_longitude_changed? }
def notify_location_changed
logger.info "#{self.id} location_updated"
logger.info "connections: #{WebsocketRails.users.users}" #this is empty - even when there is a connection open - it still pings.
channel = WebsocketRails[self.id.to_s]
WebsocketRails.logger.info "sending to the following subscribers of channel #{self.id} - #{channel.subscribers}"
channel.subscribers.each do |connection|
logger.info "sending to the following connection #{connection}"
options = {}
logger.info "#{connection.data_store}"
user = connection.data_store[:user].actable
logger.info "user: #{user.to_json}"
data = [loc_latitude: loc_latitude - user.pick_up_lat, loc_longitude: loc_longitude - user.pick_up_long].to_json
options.merge! :channel => channel.name, :token => channel.token
options[:data] = data
event = WebsocketRails::Event.new :location_updated, options
connection.trigger event
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby websocket