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.
18 19 20 |
# File 'lib/nats/io/websocket.rb', line 18 def initialize(={}) super end |
Instance Attribute Details
#socket ⇒ Object
Returns the value of attribute socket.
16 17 18 |
# File 'lib/nats/io/websocket.rb', line 16 def socket @socket end |
Instance Method Details
#connect ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nats/io/websocket.rb', line 22 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/nats/io/websocket.rb', line 47 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
57 58 59 60 61 62 63 64 65 |
# File 'lib/nats/io/websocket.rb', line 57 def read_line(deadline=nil) data = super @frame << data result = [] while msg = @frame.next result << msg end result.join end |
#setup_tls! ⇒ Object
41 42 43 44 45 |
# File 'lib/nats/io/websocket.rb', line 41 def setup_tls! return if @socket.is_a? OpenSSL::SSL::SSLSocket super end |
#write(data, deadline = nil) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/nats/io/websocket.rb', line 67 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 |