获取所有已注册连接的列表
Get list of all registered connections
这些答案对我没有帮助。
我想获取我的 ActionCable 的所有已注册连接的列表。我试过了
Redis.new.pubsub("channels", "action_cable/*")
和
ActionCable.server.connections.length
,
但他们都 return []
。所以现在我正在使用类似
的东西
def connect
self.uuid = SecureRandom.uuid
players_online = REDIS.get('players_online') || 0
players_online = players_online.to_i
players_online += 1
REDIS.set('players_online', players_online)
end
def disconnect
players_online = REDIS.get('players_online').to_i
players_online -= 1
REDIS.set('players_online', players_online)
end
但我知道这完全不是 Rails 的方式。是否有可能获得所有已注册连接的列表?
我终于找到了解决办法。
def connect
self.uuid = SecureRandom.uuid
transmit({'title': 'players_online', 'message': ActionCable.server.connections.size + 1})
ActionCable.server.connections.each do |connection|
connection.transmit({'title': 'players_online', 'message': ActionCable.server.connections.size + 1})
end
end
def disconnect
transmit({'title': 'players_online', 'message': ActionCable.server.connections.size})
ActionCable.server.connections.each do |connection|
connection.transmit({'title': 'players_online', 'message': ActionCable.server.connections.size})
end
end
ActionCable.server.connections
获取所有服务器连接的列表,当前连接到套接字的除外。这就是为什么你需要直接用 ActionCable.server.connections.size + 1
向他发送消息
这些答案对我没有帮助。
我想获取我的 ActionCable 的所有已注册连接的列表。我试过了
Redis.new.pubsub("channels", "action_cable/*")
和
ActionCable.server.connections.length
,
但他们都 return []
。所以现在我正在使用类似
def connect
self.uuid = SecureRandom.uuid
players_online = REDIS.get('players_online') || 0
players_online = players_online.to_i
players_online += 1
REDIS.set('players_online', players_online)
end
def disconnect
players_online = REDIS.get('players_online').to_i
players_online -= 1
REDIS.set('players_online', players_online)
end
但我知道这完全不是 Rails 的方式。是否有可能获得所有已注册连接的列表?
我终于找到了解决办法。
def connect
self.uuid = SecureRandom.uuid
transmit({'title': 'players_online', 'message': ActionCable.server.connections.size + 1})
ActionCable.server.connections.each do |connection|
connection.transmit({'title': 'players_online', 'message': ActionCable.server.connections.size + 1})
end
end
def disconnect
transmit({'title': 'players_online', 'message': ActionCable.server.connections.size})
ActionCable.server.connections.each do |connection|
connection.transmit({'title': 'players_online', 'message': ActionCable.server.connections.size})
end
end
ActionCable.server.connections
获取所有服务器连接的列表,当前连接到套接字的除外。这就是为什么你需要直接用 ActionCable.server.connections.size + 1