class NerdFlirt::Server
Public Class Methods
new(port)
click to toggle source
# File lib/nerd_flirt/server.rb, line 5 def initialize(port) @server = TCPServer.open(port || 8000) @connections = {} @rooms = {} @clients = {} @connections[:server] = @server @connections[:rooms] = @rooms @connections[:clients] = @clients run listen_user_messages end
Public Instance Methods
clients()
click to toggle source
# File lib/nerd_flirt/server.rb, line 56 def clients @connections[:clients] ||= {} end
listen_user_messages(username, client)
click to toggle source
# File lib/nerd_flirt/server.rb, line 40 def listen_user_messages(username, client) loop { msg = client.gets.chomp if msg.split('/')[0] == 'delete' clients.delete(msg.split('/')[1]) end @connections[:clients].each do |other_name, other_client| unless other_name == username other_client.puts "#{username.to_s}: #{msg}" end end } end
run()
click to toggle source
# File lib/nerd_flirt/server.rb, line 18 def run loop { Thread.start(@server.accept) do |client| nick_name = client.gets.chomp.to_sym @connections[:clients].each do |other_name, other_client| if nick_name == other_name || client == other_client client.puts "This username is currently taken." client.puts "Please, try again!" Thread.kill self end end puts "#{nick_name} #{client}" @connections[:clients][nick_name] = client client.puts "You are connected, thank you for joining." client.print '> '.colorize(:yellow) listen_user_messages(nick_name, client) end }.join end