Class: CatchWebSocket::Client
- Inherits:
-
Object
- Object
- CatchWebSocket::Client
- Defined in:
- lib/catch-websocket-client/client.rb
Constant Summary collapse
- FrameTimeoutSec =
600
Instance Attribute Summary collapse
-
#is_closed ⇒ Object
readonly
Returns the value of attribute is_closed.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, type = nil) ⇒ Client
constructor
A new instance of Client.
- #receive ⇒ Object
- #request(message) ⇒ Object
- #write_and_receive(message, type = nil) ⇒ Object
Constructor Details
#initialize(url, type = nil) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/catch-websocket-client/client.rb', line 23 def initialize url, type=nil @uri = URI.parse url @type = type || :text ctx = OpenSSL::SSL::SSLContext.new ctx.ssl_version = 'SSLv23' ctx. = OpenSSL::SSL::OP_NO_SSLv2 cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths ctx.cert_store = cert_store @socket = ::OpenSSL::SSL::SSLSocket.new(TCPSocket.new(@uri.host, @uri.port || 443), ctx) @socket.connect write_and_receive(@handshake = ::WebSocket::Handshake::Client.new(url: url)) @frame = ::WebSocket::Frame::Incoming::Client.new @is_closed = false end |
Instance Attribute Details
#is_closed ⇒ Object (readonly)
Returns the value of attribute is_closed.
21 22 23 |
# File 'lib/catch-websocket-client/client.rb', line 21 def is_closed @is_closed end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
21 22 23 |
# File 'lib/catch-websocket-client/client.rb', line 21 def uri @uri end |
Class Method Details
.open(url, type = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/catch-websocket-client/client.rb', line 6 def self.open url, type=nil client = CatchSocket.new url, type if block_given? begin yield client rescue => e client.close raise e end client.close client = nil end return client end |
Instance Method Details
#close ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/catch-websocket-client/client.rb', line 38 def close return if @is_closed write_and_receive(nil, :close) begin @socket.close rescue => e raise e end @is_closed = true end |
#receive ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/catch-websocket-client/client.rb', line 65 def receive while true character = nil counter = 0 while (character = @socket.getc).nil? sleep 0.1 counter += 1 raise Timeout::Error.new("FrameTimeout: #{@last_message}") if FrameTimeoutSec <= counter end if @frame.nil? @handshake << character return if @handshake.finished? else @frame << character incoming = @frame.next return incoming.data if incoming end end end |
#request(message) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/catch-websocket-client/client.rb', line 48 def request unless @is_closed = JSON.generate() if .is_a?(Hash) || .is_a?(Array) result = write_and_receive(, @type) if block_given? yield result else return result end end end |
#write_and_receive(message, type = nil) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/catch-websocket-client/client.rb', line 59 def write_and_receive , type=nil @last_message = = ::WebSocket::Frame::Outgoing::Client.new(data: , type: type, version: @handshake.version) if type @socket.write .to_s receive end |