Class: LocalMessageClient

Inherits:
Object
  • Object
show all
Defined in:
lib/local_message/local_message_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_hostname, server_port, user_name) ⇒ LocalMessageClient



7
8
9
10
11
12
# File 'lib/local_message/local_message_client.rb', line 7

def initialize(server_hostname, server_port, user_name)
  @server = server_hostname
  @port = server_port
  @username = user_name
  @listen_port = Random.rand(2000..30_000)
end

Instance Attribute Details

#listen_portObject (readonly)

Returns the value of attribute listen_port.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def listen_port
  @listen_port
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def server
  @server
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def username
  @username
end

Instance Method Details

#listenObject



24
25
26
27
28
29
30
31
# File 'lib/local_message/local_message_client.rb', line 24

def listen
  server = TCPServer.open(listen_port)
  client = server.accept
  message = client.read
  client.close
  server.close
  message
end

#registerObject



14
15
16
# File 'lib/local_message/local_message_client.rb', line 14

def register
  send_message("@register #{username} #{listen_port}")
end

#send_message(message) ⇒ Object



18
19
20
21
22
# File 'lib/local_message/local_message_client.rb', line 18

def send_message(message)
  socket = TCPSocket.open(server, port)
  socket.write(message)
  socket.close
end