|
@@ -0,0 +1,60 @@
|
|
|
|
+#!/usr/bin/ruby
|
|
|
|
+
|
|
|
|
+require 'socket'
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def displaying(client, user)
|
|
|
|
+
|
|
|
|
+ #client.puts "#{user} connected."
|
|
|
|
+ loop do
|
|
|
|
+ while line = $display_queue[user].pop
|
|
|
|
+ client.puts line
|
|
|
|
+ end
|
|
|
|
+ sleep 0.1
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def write(user, input)
|
|
|
|
+ $display_queue[user].push(input)
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+def inputting(c, user, input)
|
|
|
|
+ write(user, "You said: #{input}")
|
|
|
|
+ if input =~ /^w (.+)/
|
|
|
|
+ input = $1
|
|
|
|
+ write("paul_dev_eggdrop", "he said: #{input}")
|
|
|
|
+ end
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+def spawn_server
|
|
|
|
+ $display_queue = Hash.new
|
|
|
|
+ #$write_queue["paul_dev_eggdrop"] = ["lala", "okay"]
|
|
|
|
+
|
|
|
|
+ server = TCPServer.new 7337
|
|
|
|
+ Thread.abort_on_exception = true
|
|
|
|
+
|
|
|
|
+ loop do
|
|
|
|
+ Thread.start(server.accept) do |c|
|
|
|
|
+ user = c.gets.chomp
|
|
|
|
+ puts "#{user} connected."
|
|
|
|
+
|
|
|
|
+ $display_queue[user] = Array.new
|
|
|
|
+
|
|
|
|
+ # this thread reads what others want you to read ..
|
|
|
|
+ Thread.new{ displaying(c, user) }
|
|
|
|
+
|
|
|
|
+ # reading the client input ... to write it somewhere, possibly to the client himself.
|
|
|
|
+ while input = c.gets.chomp
|
|
|
|
+ inputting(c, user, input)
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ c.close
|
|
|
|
+ end
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+spawn_server
|