Class: OpenC3::WebSocketClientStream
- Inherits:
-
TcpipClientStream
- Object
- Stream
- TcpipSocketStream
- TcpipClientStream
- OpenC3::WebSocketClientStream
- Defined in:
- lib/openc3/streams/web_socket_client_stream.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
Attributes inherited from TcpipSocketStream
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(url, write_timeout, read_timeout, connect_timeout = 5.0) ⇒ WebSocketClientStream
constructor
A new instance of WebSocketClientStream.
- #read ⇒ Object
- #write(data, type: :text) ⇒ Object
Methods inherited from TcpipSocketStream
#connected?, #disconnect, #read_nonblock, #set_option
Methods inherited from Stream
#connected?, #disconnect, #read_nonblock
Constructor Details
#initialize(url, write_timeout, read_timeout, connect_timeout = 5.0) ⇒ WebSocketClientStream
Returns a new instance of WebSocketClientStream.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 34 def initialize(url, write_timeout, read_timeout, connect_timeout = 5.0) @url = url @uri = URI.parse @url port = ((@uri.scheme == 'wss' or @uri.scheme == 'https') ? 443 : 80) port = @uri.port if @uri.port super(@uri.host, port, port, write_timeout, read_timeout, connect_timeout) if ['https', 'wss'].include? @uri.scheme socket = ::OpenSSL::SSL::SSLSocket.new(@write_socket) socket.sync_close = true socket.hostname = @uri.host @write_socket = socket @read_socket = socket end @headers = {} end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
26 27 28 |
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 26 def headers @headers end |
Instance Method Details
#connect ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 50 def connect super() @handshake = ::WebSocket::Handshake::Client.new(:url => @url, :headers => @headers) @frame = ::WebSocket::Frame::Incoming::Client.new @handshaked = false @write_socket.write(@handshake.to_s) read() # This should wait for the handshake return true end |
#read ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 60 def read while true if @handshaked msg = @frame.next return msg.data if msg end data = super() return data if data.length <= 0 if @handshaked @frame << data msg = @frame.next return msg.data if msg else index = 0 chars = "" data.each_char do |char| @handshake << char chars << char index += 1 if @handshake.finished? @handshaked = true break end end if @handshaked data = data[index..-1] @frame << data return end end end end |
#write(data, type: :text) ⇒ Object
95 96 97 98 |
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 95 def write(data, type: :text) frame = ::WebSocket::Frame::Outgoing::Client.new(:data => data, :type => type, :version => @handshake.version) super(frame.to_s) end |