rubymultithreadingchatbackgroundworker

How to run a background thread in ruby?


I am new to ruby and thought it would be a great idea to rebuild a simple chat program I made in C#.

I am using Ruby 2.0.0 MRI (Matz’s Ruby Implementation).

The problem is I want to have I/O for simple server commands while the server is running. This is the server that was taken from the sample. I added the commands method that uses gets() to get input. I want this method to run as a thread in the background, but the thread is blocking the other thread.

require 'socket'                # Get sockets from stdlib

server = TCPServer.open(2000)   # Socket to listen on port 2000

def commands
    x = 1
    while x == 1
        exitProgram = gets.chomp
        if exitProgram == "exit" || exitProgram == "Exit"
            x = 2
            abort("Exiting the program.")
        end
    end
end

def main
    Thread.start(commands)
    Thread.start(server.accept) 
    loop {                          # Servers run forever

        Thread.start(server.accept) do |client|
        client.puts(Time.now.ctime) # Send the time to the client
        client.puts "Closing the connection. Bye!"
        client.close                # Disconnect from the client
      end
    }
end

main

This is the client so far.

require 'socket'      # Sockets are in standard library

hostname = 'localhost'
port = 2000

s = TCPSocket.open(hostname, port)

while line = s.gets   # Read lines from the socket
  puts line.chop      # And print with platform line terminator
end
s.close               # Close the socket when done
gets.chomp

Solution

  • Read the documentation for Thread.new (which is the same as Thread.start here)

    Thread.start(commands) runs the commands method and passes its return value to a thread (which then does nothing). It's blocking because you aren't starting any threads when gets is called. You want

    Thread.start { commands }
    

    Here's a similar demo script that works just like you would expect

    def commands
      while gets.strip !~ /^exit$/i
        puts "Invalid command"
      end
      abort "Exiting the program"
    end
    
    Thread.start { commands }
    
    loop do
      puts "Type exit:"
      sleep 2
    end