Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'socket'
- include Socket::Constants
- $ApplicationPort = 9319
- class ChatClient
- def initialize
- end
- def connect(host = 'localhost', port = $ApplicationPort)
- @socket = Socket.new(AF_INET, SOCK_STREAM, 0)
- @socket_adress = Socket.sockaddr_in(port, host)
- @socket.connect(@socket_adress)
- end
- def send(msg)
- @socket.puts(msg)
- end
- def read
- connect if @socket.nil?
- return false if @socket.closed? # or @socket.eof?
- if IO.select([@socket], nil, nil, 2)
- #@buffer += @socket.readpartial(4096)
- puts "[ServerSays]: "+@socket.gets
- #@game = YAML::load(msg)
- #puts 'game loaded'
- return true
- end
- end
- def main
- loop do
- read
- sleep 1
- end
- end
- end
- chat_client = ChatClient.new
- chat_client.connect
- ['Hey there', 'I like pie', 'What is your name', 'Thats a nice name'].each do |m|
- chat_client.send(m)
- sleep(1)
- end
- chat_client.main
- #chat_client.main
Add Comment
Please, Sign In to add comment