Class: Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = 'localhost', port = 4711) ⇒ Connection



4
5
6
7
8
9
10
11
12
# File 'lib/connection.rb', line 4

def initialize (server = 'localhost', port = 4711)
  begin
    @socket = TCPSocket.new server, port
  rescue
    puts "Error connecting to Minecraft. Is it running?\n\n"
    @socket.close if @socket
    abort("#{$!}")
  end
end

Class Method Details

.finalize(id) ⇒ Object



28
29
30
# File 'lib/connection.rb', line 28

def Connection.finalize(id)
  @socket.close if @socket
end

Instance Method Details

#send_command(command) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/connection.rb', line 14

def send_command(command)
  if @socket
    @socket.puts command
  else
    puts "Not connected to Minecraft Pi! Is it running?"
  end
end

#send_with_response(command) ⇒ Object



22
23
24
25
# File 'lib/connection.rb', line 22

def send_with_response(command)
  send_command command
  return @socket.gets.chomp
end