-
Notifications
You must be signed in to change notification settings - Fork 18
/
server_threads.rb
48 lines (40 loc) · 1.18 KB
/
server_threads.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
require 'socket'
require './lib/threads'
Thread.abort_on_exception = true
puts "Starting server on port 2000 with pid #{Process.pid}"
server = TCPServer.open(2000)
mutex = Mutex.new
messages = []
loop do
# Accept incoming connections and spawn a thread for each one
Thread.new(server.accept) do |socket|
nickname = read_line_from(socket)
puts "Accepted connection from #{nickname}"
# Run another thread that sends incoming messages back to the client
Thread.new do
sent_until = Time.now
loop do
messages_to_send = mutex.synchronize do
get_messages_to_send(nickname, messages, sent_until).tap do
sent_until = Time.now
end
end
messages_to_send.each do |message|
socket.puts "#{message[:nickname]}: #{message[:text]}"
end
sleep 0.2
end
end
# Listen for messages from the client and add these to the messages list
while incoming = read_line_from(socket)
mutex.synchronize do
messages.push(
:time => Time.now,
:nickname => nickname,
:text => incoming
)
end
end
puts "Disconnected #{nickname}"
end
end