Sha256: c0c6bb86c7283502b348e521586de2dc2af2f3e2214a71c1c4e908fa86d9ce68

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "wampproto"
require_relative "websocket_client"

module Wamp
  module Connection
    # Conn
    class WebSocketConnection < Session
      attr_reader :url, :websocket

      def initialize(url = "ws://localhost:8080/ws", joiner = Wampproto::Joiner.new("realm1"))
        super(joiner)
        @url        = url
        @websocket  = Wamp::Connection::WebsocketClient.new(self, protocols)
      end

      def run
        websocket.run
      ensure
        p %i[run close]
        websocket.close
      end

      def on_open
        transmit joiner.send_hello
      end

      def transmit(data)
        websocket.transmit data
      end

      def on_close(reason, code)
        p [:on_close, reason, code]
      end

      def on_error; end

      private

      def protocols
        case joiner.serializer.name
        when Wampproto::Serializer::JSON.name then "wamp.2.json"
        when Wampproto::Serializer::Msgpack.name then "wamp.2.msgpack"
        when Wampproto::Serializer::Cbor.name then "wamp.2.cbor"
        end.then do |protocol|
          [protocol]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xconn-0.1.2 lib/wamp/connection/websocket_connection.rb
xconn-0.1.1 lib/wamp/connection/websocket_connection.rb
xconn-0.1.0 lib/wamp/connection/websocket_connection.rb