forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_simple_chat_server.rb
149 lines (113 loc) · 3.25 KB
/
03_simple_chat_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env ruby
require 'rubygems' # or use Bundler.setup
require 'eventmachine'
class SimpleChatServer < EM::Connection
@@connected_clients = Array.new
DM_REGEXP = /^@([a-zA-Z0-9]+)\s*:?\s*(.+)/.freeze
attr_reader :username
#
# EventMachine handlers
#
def post_init
@username = nil
puts "A client has connected..."
ask_username
end
def unbind
@@connected_clients.delete(self)
puts "[info] #{@username} has left" if entered_username?
end
def receive_data(data)
if entered_username?
handle_chat_message(data.strip)
else
handle_username(data.strip)
end
end
#
# Username handling
#
def entered_username?
end # entered_username?
def handle_username(input)
if input.empty?
send_line("Blank usernames are not allowed. Try again.")
ask_username
else
@username = input
@@connected_clients.push(self)
self.other_peers.each { |c| c.send_data("#{@username} has joined the room\n") }
puts "#{@username} has joined"
self.send_line("[info] Ohai, #{@username}")
end
end # handle_username(input)
def ask_username
self.send_line("[info] Enter your username:")
end # ask_username
#
# Message handling
#
def handle_chat_message(msg)
if command?(msg)
self.handle_command(msg)
else
if direct_message?(msg)
self.handle_direct_message(msg)
else
self.announce(msg, "#{@username}:")
end
end
end # handle_chat_message(msg)
def direct_message?(input)
input =~ DM_REGEXP
end # direct_message?(input)
def handle_direct_message(input)
username, message = parse_direct_message(input)
if connection = @@connected_clients.find { |c| c.username == username }
puts "[dm] @#{@username} => @#{username}"
connection.send_line("[dm] @#{@username}: #{message}")
else
send_line "@#{username} is not in the room. Here's who is: #{usernames.join(', ')}"
end
end # handle_direct_message(input)
def parse_direct_message(input)
return [$1, $2] if input =~ DM_REGEXP
end # parse_direct_message(input)
#
# Commands handling
#
def command?(input)
input =~ /(exit|status)$/i
end # command?(input)
def handle_command(cmd)
case cmd
when /exit$/i then self.close_connection
when /status$/i then self.send_line("[chat server] It's #{Time.now.strftime('%H:%M')} and there are #{self.number_of_connected_clients} people in the room")
end
end # handle_command(cmd)
#
# Helpers
#
def announce(msg = nil, prefix = "[chat server]")
@@connected_clients.each { |c| c.send_line("#{prefix} #{msg}") } unless msg.empty?
end # announce(msg)
def number_of_connected_clients
@@connected_clients.size
end # number_of_connected_clients
def other_peers
@@connected_clients.reject { |c| self == c }
end # other_peers
def send_line(line)
self.send_data("#{line}\n")
end # send_line(line)
def usernames
@@connected_clients.map { |c| c.username }
end # usernames
end
EventMachine.run do
# hit Control + C to stop
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
end