Sha256: bcc609b67198b4a93107e5d91315a8411322a0513fddae15ee3d738c9ea44bb7

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

begin
  require 'websocket'
rescue LoadError
  raise LoadError, "Please add `websocket` gem to your Gemfile to connect to NATS via WebSocket."
end

module NATS
  module IO
    # WebSocket to connect to NATS via WebSocket and automatically decode and encode frames.

    # @see https://docs.nats.io/running-a-nats-service/configuration/websocket

    class WebSocket < Socket
      class HandshakeError < RuntimeError; end

      attr_accessor :socket

      def initialize(options={})
        super
      end

      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

      def setup_tls!
        return if @socket.is_a? OpenSSL::SSL::SSLSocket

        super
      end

      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

      def read_line(deadline=nil)
        data = super
        @frame << data
        result = []
        while msg = @frame.next
          result << msg
        end
        result.join
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nats-pure-2.4.0 lib/nats/io/websocket.rb
nats-pure-2.3.0 lib/nats/io/websocket.rb