D
D
Denis Savitsky2012-05-03 19:19:26
ruby
Denis Savitsky, 2012-05-03 19:19:26

Test ruby ​​TCPSocket server?

There is a server based on GServer.
There are also clients that send messages to the server.
Messages should be sent to other clients connected to the server.

How to test simultaneous connections with rspec (when one client sends while others listen)?

Sources:

# Handle each connection<br>
def serve(io)<br>
  io.puts("LOGIN\n")<br>
  # Listen for identifier<br>
  user = io.gets.chomp<br>
<br>
  ...<br>
<br>
  # Add connection to the list<br>
  @mutex.synchronize { @chatters[user] = io }<br>
<br>
  # Get and broadcast input<br>
  loop do<br>
<br>
    incoming = io.gets<br>
    broadcast(incoming, io)<br>
<br>
  end<br>
end<br>
<br>
#Send message out to everyone, but sender<br>
def broadcast(message="", sender)<br>
<br>
  # Mutex for safety - GServer uses threads<br>
  @mutex.synchronize do<br>
<br>
    @chatters.each do |chatter|<br>
<br>
      socket = chatter[1]<br>
<br>
      # Do not send to sender<br>
      if sock != sender<br>
<br>
        sock.print(message)<br>
<br>
      end<br>
<br>
    end<br>
<br>
  end<br>
end

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question