Module: EM::JsonConnection::Base

Included in:
Client, Server
Defined in:
lib/em_json_connection.rb

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/em_json_connection.rb', line 33

def connected?
  @connect_status
end

#init_parserObject



13
14
15
16
# File 'lib/em_json_connection.rb', line 13

def init_parser
  @parser = Yajl::Parser.new(:symbolize_keys => true)
  @parser.on_parse_complete = method(:json_parsed)
end

#json_parsed(hash) ⇒ Object



25
26
27
# File 'lib/em_json_connection.rb', line 25

def json_parsed(hash)
  puts 'to be implemented in the subclass'
end

#post_initObject



7
8
9
10
11
# File 'lib/em_json_connection.rb', line 7

def post_init
  init_parser
  @encoder = Yajl::Encoder.new
  @connect_status = true
end

#receive_data(data) ⇒ Object



18
19
20
21
22
23
# File 'lib/em_json_connection.rb', line 18

def receive_data(data)
  @parser << data
rescue Yajl::ParseError => error
  puts "Yajl::ParseError: #{error}"
  init_parser
end

#send_data(data) ⇒ Object



29
30
31
# File 'lib/em_json_connection.rb', line 29

def send_data(data)
  super @encoder.encode(data)
end

#unbindObject



37
38
39
# File 'lib/em_json_connection.rb', line 37

def unbind
  @connect_status = false
end