From e046a51a7f9d4f9ce67374e595f92d13fd30df25 Mon Sep 17 00:00:00 2001 From: Ismail Akram Date: Tue, 4 Jun 2024 18:55:12 +0500 Subject: [PATCH] Server advertises supported protocols to the client --- lib/wamp/router/connection.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/wamp/router/connection.rb b/lib/wamp/router/connection.rb index 1bafa15..a83ee87 100644 --- a/lib/wamp/router/connection.rb +++ b/lib/wamp/router/connection.rb @@ -18,14 +18,15 @@ class Connection < Client # rubocop:disable Metrics/ClassLength OPEN = 1 CLOSING = 2 CLOSED = 3 + SUPPORTED_PROTOCOLS = ["wamp.2.msgpack", "wamp.2.cbor", "wamp.2.json"].freeze attr_reader :socket, :session, :acceptor - def initialize(socket, &cleanup) - super() + def initialize(socket, &cleanup) # rubocop:disable Lint/MissingSuper + # super() # on_connect() does what super() does @cleanup = cleanup @socket = socket - @driver = WebSocket::Driver.server(self) + @driver = WebSocket::Driver.server(self, protocols: SUPPORTED_PROTOCOLS) @driver.on(:open) { on_open(_1) } @driver.on(:message) { on_message(_1.data) } @driver.on(:close) { |evt| begin_close(evt.reason, evt.code) } @@ -57,7 +58,7 @@ def finalize_close return if @ready_state == CLOSED @ready_state = CLOSED - @driver.close + @driver.close(*@close_params) socket.close end @@ -128,21 +129,28 @@ def on_close(message) @driver.close(message.code, message.reason) end - def close(_code, _reason) - @driver.close + def close(code, reason) + @driver.close(reason, code) end attr_reader :serializer - def choose_serializer_from(protocols) - @serializer = if protocols.include?("wamp.2.msgpack") + def choose_serializer_from(protocols) # rubocop:disable Metrics/MethodLength + common_protocols = protocols.to_s.split(",") & SUPPORTED_PROTOCOLS + protocol = common_protocols[0].to_s + + @serializer = if protocol.include?("wamp.2.msgpack") Wampproto::Serializer::Msgpack elsif protocols.include?("wamp.2.cbor") Wampproto::Serializer::Cbor elsif protocols.include?("wamp.2.json") Wampproto::Serializer::JSON else - close + reason = "on_connect: protocols not supported '#{protocols}'." + code = 1006 + puts reason + @close_params = [reason, code] + finalize_close end end end