implement alternative match-making approach with Lua scripts in Redis
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# Real-time matchmaking channel.
|
||||
#
|
||||
# Client flow:
|
||||
# 1. Connect: ws(s)://host/cable?token=<JWT>
|
||||
# 2. Subscribe to "MatchmakingChannel"
|
||||
# 3. Send { action: "start", rating: <optional int> } to enter the queue
|
||||
# 4. Receive { type: "matched", ... } or { type: "timeout" }
|
||||
# 5. Disconnecting (or sending { action: "stop" }) cancels the search.
|
||||
class MatchmakingChannel < ApplicationCable::Channel
|
||||
def subscribed
|
||||
stream_for current_user
|
||||
|
||||
# If a match was found while this user had no subscription
|
||||
# (e.g. brief reconnect), deliver it now — exactly once.
|
||||
if match = MatchmakingService.claim(current_user.id)
|
||||
transmit(match)
|
||||
end
|
||||
end
|
||||
|
||||
def start(data)
|
||||
MatchmakingService.enqueue(current_user.id, rating: data["rating"])
|
||||
end
|
||||
|
||||
def stop
|
||||
MatchmakingService.cancel(current_user.id)
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
MatchmakingService.cancel(current_user.id)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user