メモリ内メッセージ ディスパッチャーから始めてみてください。
require 'thread'
class Room
def initialize
@users = []
end
def join(user)
@users << user
end
def leave(user)
@user.delete(user)
end
def broadcast(message)
@users.each do |user|
user.enqueue(message)
end
end
end
class User
def initialize
@mutex = Mutex.new
@queued_messages = []
end
def enqueue(message)
@mutex.synchronize do
@queued_message << message
end
end
def get_new_messages
@mutex.synchronize do
output = @queued_messages
@queued_messages = []
end
return output
end
end
アップデート
ROOM = Room.new
class Connection
def user_logged_in
# @user = ...
ROOM.join(@user)
end
def received_message(message)
ROOM.broadcast(message)
end
def receive_send_more_messages_request(req)
messages = @user.get_new_messages
# write messages
end
def connection_closed
ROOM.leave(@user)
end
end