Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
require 'socket' HOST = 'localhost' PORT = 4505 server = TCPServer.new(HOST, PORT) # array to store all the active connections sessions = [] while (session = server.accept) # push the current session(socket) in the array sessions << session # initialize a new thead for each connection Thread.new(session) do |local_session| # each time a client sends some data send it to all the connections while(true) data = local_session.gets sessions.each do |s| begin s.puts data rescue Errno::ECONNRESET # an exception is raised, that means the connection to the client is broken sessions.delete(s) end end end end end
This paste will be private.
From the Design Piracy series on my blog: