Guest User

Untitled

a guest
Apr 21st, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. require 'socket'
  2. include Socket::Constants
  3.  
  4. $ApplicationPort = 9319
  5.  
  6. class ChatClient
  7. def initialize
  8.  
  9. end
  10.  
  11. def connect(host = 'localhost', port = $ApplicationPort)
  12. @socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  13. @socket_adress = Socket.sockaddr_in(port, host)
  14. @socket.connect(@socket_adress)
  15. end
  16.  
  17. def send(msg)
  18. @socket.puts(msg)
  19. end
  20.  
  21. def read
  22. connect if @socket.nil?
  23. return false if @socket.closed? # or @socket.eof?
  24. if IO.select([@socket], nil, nil, 2)
  25. #@buffer += @socket.readpartial(4096)
  26. puts "[ServerSays]: "+@socket.gets
  27. #@game = YAML::load(msg)
  28. #puts 'game loaded'
  29. return true
  30. end
  31. end
  32.  
  33. def main
  34. loop do
  35. read
  36. sleep 1
  37. end
  38. end
  39. end
  40.  
  41. chat_client = ChatClient.new
  42. chat_client.connect
  43.  
  44. ['Hey there', 'I like pie', 'What is your name', 'Thats a nice name'].each do |m|
  45. chat_client.send(m)
  46. sleep(1)
  47. end
  48.  
  49. chat_client.main
  50.  
  51. #chat_client.main
Add Comment
Please, Sign In to add comment