我正在使用websocket-rails gem进行我的rails应用程序的websocket连接 - 事情是它在我的计算机上完美运行 - 但在我的生产机器上它将无法正常工作。
当我连接时 - 它写入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