Class: Onebot::WebSocket::Client

Inherits:
Bot
  • Object
show all
Defined in:
lib/Core/WebSocket/Client.rb

Instance Attribute Summary collapse

Attributes inherited from Bot

#selfID

Instance Method Summary collapse

Methods inherited from Bot

#method_missing, #respond_to_missing?, #sendMessage

Constructor Details

#initialize(url: nil, logger: nil, **args) ⇒ Client

设置 WS URL



12
13
14
15
16
# File 'lib/Core/WebSocket/Client.rb', line 12

def initialize(url: nil, logger: nil, **args)
  super
  @eventLogger = Logging::EventLogger.new(logger)
  @url = url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Onebot::WebSocket::Bot

Instance Attribute Details

#apiObject

Returns the value of attribute api.



9
10
11
# File 'lib/Core/WebSocket/Client.rb', line 9

def api
  @api
end

#urlURI

Returns WS URL.

Returns:

  • WS URL



6
7
8
# File 'lib/Core/WebSocket/Client.rb', line 6

def url
  @url
end

#wsFaye::WebSocket::Client

Returns WS Conn.

Returns:

  • WS Conn



8
9
10
# File 'lib/Core/WebSocket/Client.rb', line 8

def ws
  @ws
end

Instance Method Details

#connect(protocols = nil, options = {}) ⇒ Object

连接 WS



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/Core/WebSocket/Client.rb', line 19

def connect(protocols = nil, options = {})
  @eventLogger.log '正在连接到 ' << @url
  EM.run do
    @ws = Faye::WebSocket::Client.new(@url, protocols, options)
    @api = API.new(@ws, @eventLogger)

    @ws.on :message do |event|
      Thread.new { dataParse(event.data) }
    end

    @ws.on :close do |event|
      emit :close, event
      @eventLogger.log '连接断开'
      @ws = nil
      EM.stop
    end

    @ws.on :error do |event|
      emit :error, event
      @ws = nil
    end
  end
end