class GameOverseer::Client
Constants
- CHAT
- FAULT
- HANDSHAKE
- VERSION
- WORLD
Attributes
socket[R]
Public Class Methods
instance()
click to toggle source
# File lib/gameoverseer/client.rb, line 34 def self.instance @instance end
instance=(instance)
click to toggle source
# File lib/gameoverseer/client.rb, line 37 def self.instance=instance @instance = instance end
new(host = "localhost", port = 56789, compression = true)
click to toggle source
# File lib/gameoverseer/client.rb, line 19 def initialize(host = "localhost", port = 56789, compression = true) @host = host @port = port @compression = compression GameOverseer::Client.instance = self @service_manager = ServiceManager.new @socket = ENet::Connection.new(host, port, 4, 0, 0) @socket.use_compression(compression) @socket.on_packet_receive(method(:handle_packet)) connect end
Public Instance Methods
connect(timeout = 1000)
click to toggle source
# File lib/gameoverseer/client.rb, line 41 def connect(timeout = 1000) # default to 1 second tries = 0 begin @socket.connect(timeout) rescue StandardError tries+=1 retry unless tries > 3 raise if tries > 3 end end
connected?()
click to toggle source
# File lib/gameoverseer/client.rb, line 56 def connected? @socket.connected? end
disconnect(timeout = 1000)
click to toggle source
# File lib/gameoverseer/client.rb, line 66 def disconnect(timeout = 1000) tries = 0 begin @socket.disconnect(timeout) rescue StandardError tries+=1 retry unless tries > 3 raise if tries > 3 end end
handle_packet(data, channel)
click to toggle source
# File lib/gameoverseer/client.rb, line 60 def handle_packet(data, channel) _data= data.chomp.strip data = MultiJson.load(_data) ServiceManager.instance.handle_packet(data, channel) end
online?()
click to toggle source
# File lib/gameoverseer/client.rb, line 52 def online? @socket.online? end
transmit(channel, mode, data = nil, channel_id = CHAT, reliable = false)
click to toggle source
# File lib/gameoverseer/client.rb, line 77 def transmit(channel, mode, data = nil, channel_id = CHAT, reliable = false) raise "channel must be a String" unless channel.is_a?(String) raise "mode must be a String" unless mode.is_a?(String) raise "data must be a Hash" unless data.is_a?(Hash) or data == nil raise "reliable must be a Boolean" unless reliable.is_a?(TrueClass) or reliable.is_a?(FalseClass) if data message = MultiJson.dump({channel: channel, mode: mode, data: data}) else message = MultiJson.dump({channel: channel, mode: mode}) end @socket.send_packet(message, reliable, channel_id) end
update(timeout = 0)
click to toggle source
# File lib/gameoverseer/client.rb, line 91 def update(timeout = 0) @socket.update(timeout) end