Class: NATS::IO::WebSocket
Overview
Defined Under Namespace
Classes: HandshakeError
Instance Attribute Summary collapse
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #read(max_bytes = MAX_SOCKET_READ_BYTES, deadline = nil) ⇒ Object
- #read_line(deadline = nil) ⇒ Object
- #setup_tls! ⇒ Object
- #write(data, deadline = nil) ⇒ Object
Methods inherited from Socket
Constructor Details
#initialize(options = {}) ⇒ WebSocket
Returns a new instance of WebSocket.
20 21 22 |
# File 'lib/nats/io/websocket.rb', line 20 def initialize( = {}) super end |
Instance Attribute Details
#socket ⇒ Object
Returns the value of attribute socket.
18 19 20 |
# File 'lib/nats/io/websocket.rb', line 18 def socket @socket end |
Instance Method Details
#connect ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nats/io/websocket.rb', line 24 def connect super setup_tls! if @uri.scheme == "wss" # WebSocket connection must be made over TLS from the beginning @handshake = ::WebSocket::Handshake::Client.new url: @uri.to_s @frame = ::WebSocket::Frame::Incoming::Client.new @handshaked = false @socket.write @handshake.to_s until @handshaked @handshake << method(:read).super_method.call(MAX_SOCKET_READ_BYTES) if @handshake.finished? @handshaked = true end end end |
#read(max_bytes = MAX_SOCKET_READ_BYTES, deadline = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/nats/io/websocket.rb', line 49 def read(max_bytes = MAX_SOCKET_READ_BYTES, deadline = nil) data = super @frame << data result = [] while (msg = @frame.next) result << msg end result.join end |
#read_line(deadline = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/nats/io/websocket.rb', line 59 def read_line(deadline = nil) data = super @frame << data result = [] while (msg = @frame.next) result << msg end result.join end |
#setup_tls! ⇒ Object
43 44 45 46 47 |
# File 'lib/nats/io/websocket.rb', line 43 def setup_tls! return if @socket.is_a? OpenSSL::SSL::SSLSocket super end |
#write(data, deadline = nil) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/nats/io/websocket.rb', line 69 def write(data, deadline = nil) raise HandshakeError, "Attempted to write to socket while WebSocket handshake is in progress" unless @handshaked frame = ::WebSocket::Frame::Outgoing::Client.new(data: data, type: :binary, version: @handshake.version) super(frame.to_s) end |