/
server.rb
48 lines (41 loc) · 1.24 KB
/
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
module ActiveSocket
class ActiveSocketServer
include Processors
def initialize(port, environment = :production)
ActiveSocket.const_set("ENV", environment)
@serverSocket = TCPServer.new(port)
@serverSocket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
ActiveSocket.log.debug("ActiveSocket Server started on port #{port.to_s} in #{ActiveSocket::ENV} mode")
puts "ActiveSocket Server started on port #{port.to_s} in #{ActiveSocket::ENV} mode"
while 1
wait_for_client
end
end
private
def wait_for_client
ActiveSocket.log.debug("waiting for a client")
while (@session = @serverSocket.accept)
help_client
return if @session.closed?
end
end
def help_client
ActiveSocket.log.debug("helping a client")
while 1
wait_for_message
return if @session.closed?
end
end
def wait_for_message
if @session.eof?
ActiveSocket.log.debug("client disconnected :(")
@session.close
return
else
message = @session.gets
response = process_message(message)
@session.write(response) unless @session.closed?
end
end
end #server
end #module