Last active
August 29, 2015 14:01
-
-
Save tompng/91b6d1db34d7021d17e1 to your computer and use it in GitHub Desktop.
telnetでみんなでshiritori $ telnet localhost 1234
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'shiritori' | |
queue = Queue.new | |
shiritori = Shiritori::Main.new | |
sockets = [] | |
shiritori.define_singleton_method :get_command do | |
queue.deq | |
end | |
outputs = ['Please input first object >'] | |
[shiritori, $stdout].product([:puts, :print]).each do |obj, name| | |
obj.define_singleton_method name do |msg| | |
outputs << msg | |
sockets.each do |s| | |
begin | |
s.puts msg | |
rescue | |
end | |
end | |
end | |
end | |
server = TCPServer.new 1234 | |
Thread.new do | |
loop do | |
Thread.start(server.accept) do |s| | |
s.write outputs.join("\n") | |
sockets.push s | |
while cmd=s.gets | |
queue.enq cmd | |
end | |
sockets.delete s | |
s.close | |
end | |
end | |
end | |
shiritori.start ARGV | |
Process.exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment