Class: MCP::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mcp/connection.rb', line 7

def initialize(opts = {}, &block)
  host = opts[:host] || "localhost"
  port = opts[:port] || 25565
  @socket = TCPSocket.new host,port
   = LoginRequest.new
  .nick = "testing221"
  @socket << .with_id
  map = Util.mapping

  while true
    id = MCUbyte.read(@socket).to_i
    klass = map[id]
    if !klass
      @socket.close
      raise Exception, "Unknown packet: 0x#{id.to_s(16).upcase}"
      return
    end
    p klass
    a = klass.read(@socket)
    p a
    if a.instance_of? LoginResponse
      puts "Polaczono z #{host}:#{port}, entity_id=#{a.entity_id}"
    elsif a.instance_of? Kick
      @socket.close
      raise Exception, "Got kick: #{a.reason}"
      return
    elsif a.instance_of? Keepalive
      k = Keepalive.new
      k.id = a.id
      @socket << k.with_id
    end
    puts "Naste[ny packiet"

  end
end