我正在尝试实现一项功能,用户可以在登录后知道某人是否在线。我正在对WebSocket使用Anycable。当用户访问网站时,WebSocket会将用户ID添加到Redis,如果用户断开与网站的连接,WebSocket将删除用户ID。现在,一段时间后,或者如果我多次刷新网站,它将引发错误“订阅错误:应用程序错误:ERR达到最大客户端数”。该网站已部署在heroku上。
我评论了向Redis添加和删除用户ID的部分,并且不再抛出任何错误。
def subscribed
stream_from "appearances_channel"
redis = Redis.new
puts user_id
online = true
puts online
online ? redis.set("user_#{user_id}_online", "1") : redis.del("user_#{user_id}_online")
ActionCable.server.broadcast "appearances_channel",
user_id: user_id,
online: online
end
def unsubscribed
redis = Redis.new
online = false
online ? redis.set("user_#{user_id}_online", "1") : redis.del("user_#{user_id}_online")
ActionCable.server.broadcast "appearances_channel",
user_id: user_id,
online: online
end
错误日志:
2019-08-06T13:26:37.867650+00:00 app[web.1]: [AnyCable sid=kn3TwGsIbbqA5AXX3UHWKp] RPC Disconnect: <AnyCable::DisconnectRequest: identifiers: "{\"__ltags__\":[\"ActionCable\",\"1\"],\"current_user\":\"1\"}", subscriptions: [], path: "/cable?id=1", headers: {"cookie"=>""}>
2019-08-06T13:26:37.867978+00:00 app[web.1]: [AnyCable sid=kn3TwGsIbbqA5AXX3UHWKp] [ActionCable] [1] Finished "/cable?id=1" [AnyCable] for at 2019-08-06 13:26:37 +0000 (Closed)
2019-08-06T13:26:38.817847+00:00 app[web.1]: [AnyCable sid=RN4~mA8EX6PmMO2tR83Ei6] RPC Connect: <AnyCable::ConnectionRequest: path: "/cable?id=1", headers: {"cookie"=>""}>
2019-08-06T13:26:38.818099+00:00 app[web.1]: [AnyCable sid=RN4~mA8EX6PmMO2tR83Ei6] Started "/cable?id=1" [AnyCable] for at 2019-08-06 13:26:38 +0000
2019-08-06T13:26:39.053969+00:00 app[web.1]: [AnyCable sid=RN4~mA8EX6PmMO2tR83Ei6] RPC Command: <AnyCable::CommandMessage: command: "subscribe", identifier: "{\"channel\":\"AppearanceChannel\"}", connection_identifiers: "{\"__ltags__\":[\"ActionCable\",\"1\"],\"current_user\":\"1\"}", data: "">
2019-08-06T13:26:39.054228+00:00 app[web.1]: 1
2019-08-06T13:26:39.054245+00:00 app[web.1]: true
2019-08-06T13:26:39.131043+00:00 app[web.1]: [AnyCable
sid=RN4~mA8EX6PmMO2tR83Ei6] ERR max number of clients reached
2019-08-06T13:26:39.131313+00:00 app[web.1]: E 2019-08-06T13:26:39.131Z context=node sid=RN4~mA8EX6PmMO2tR83Ei6 Subscribe error: Application error: ERR max number of clients reached