Class: Onebot::WebSocket::Client
- Defined in:
- lib/Core/WebSocket/Client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#url ⇒ URI
WS URL.
-
#ws ⇒ Faye::WebSocket::Client
WS Conn.
Attributes inherited from Bot
Instance Method Summary collapse
-
#connect(protocols = nil, options = {}) ⇒ Object
连接 WS.
-
#initialize(url: nil, logger: nil, **args) ⇒ Client
constructor
设置 WS URL.
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
#api ⇒ Object
Returns the value of attribute api.
9 10 11 |
# File 'lib/Core/WebSocket/Client.rb', line 9 def api @api end |
#url ⇒ URI
Returns WS URL.
6 7 8 |
# File 'lib/Core/WebSocket/Client.rb', line 6 def url @url end |
#ws ⇒ Faye::WebSocket::Client
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, = {}) @eventLogger.log '正在连接到 ' << @url EM.run do @ws = Faye::WebSocket::Client.new(@url, protocols, ) @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 |