Skip to content

Commit

Permalink
Merge pull request #20 from xconnio/advertise-protocols
Browse files Browse the repository at this point in the history
Server advertises supported protocols to the client
  • Loading branch information
rubyonrails3 authored Jun 4, 2024
2 parents f3a5012 + e046a51 commit f748e9e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/wamp/router/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -57,7 +58,7 @@ def finalize_close
return if @ready_state == CLOSED

@ready_state = CLOSED
@driver.close
@driver.close(*@close_params)
socket.close
end

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f748e9e

Please sign in to comment.