socket_poc.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/ruby
  2. require 'socket'
  3. def displaying(client, user)
  4. #client.puts "#{user} connected."
  5. loop do
  6. while line = $display_queue[user].pop
  7. client.puts line
  8. end
  9. sleep 0.1
  10. end
  11. end
  12. def write(user, input)
  13. $display_queue[user].push(input)
  14. end
  15. def inputting(c, user, input)
  16. write(user, "You said: #{input}")
  17. if input =~ /^w (.+)/
  18. input = $1
  19. write("paul_dev_eggdrop", "he said: #{input}")
  20. end
  21. end
  22. def spawn_server
  23. $display_queue = Hash.new
  24. #$write_queue["paul_dev_eggdrop"] = ["lala", "okay"]
  25. server = TCPServer.new 7337
  26. Thread.abort_on_exception = true
  27. loop do
  28. Thread.start(server.accept) do |c|
  29. user = c.gets.chomp
  30. puts "#{user} connected."
  31. $display_queue[user] = Array.new
  32. # this thread reads what others want you to read ..
  33. Thread.new{ displaying(c, user) }
  34. # reading the client input ... to write it somewhere, possibly to the client himself.
  35. while input = c.gets.chomp
  36. inputting(c, user, input)
  37. end
  38. c.close
  39. end
  40. end
  41. end
  42. spawn_server