Class: CQHTTP::Service

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

Overview

TODO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname: '0.0.0.0', port: 9455) ⇒ Service

Returns a new instance of Service.



6
7
8
9
# File 'lib/CQHTTP/service.rb', line 6

def initialize(hostname: '0.0.0.0', port: 9455)
  @server = TCPServer.new(hostname, port)
  @event_method = []
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/CQHTTP/service.rb', line 4

def json
  @json
end

Instance Method Details

#accept(socket) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/CQHTTP/service.rb', line 22

def accept(socket)
  head socket

  socket.print "HTTP/1.1 204\r\n" \
    "Content-Type: application/json\r\n" \
    "Connection: Close\r\n\r\n"
  data = socket.gets
  puts data
  @json = JSON.parse data

  @event_method.each { |func| func.call self }

  socket.close
end

#bind(func) ⇒ Object



11
12
13
# File 'lib/CQHTTP/service.rb', line 11

def bind(func)
  @event_method << func
end

#runObject



15
16
17
18
19
20
# File 'lib/CQHTTP/service.rb', line 15

def run
  loop do
    socket = @server.accept
    accept socket
  end
end