diff --git a/lib/wamp/connection/session.rb b/lib/wamp/connection/session.rb new file mode 100644 index 0000000..5ca3ad0 --- /dev/null +++ b/lib/wamp/connection/session.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Wamp + module Connection + # Client Session + class Session + attr_reader :joiner, :session, :store, :api + attr_accessor :executor, :stream + + def initialize(joiner = Wampproto::Joiner.new("realm1")) + @joiner = joiner + @session = Wampproto::Session.new(joiner.serializer) + @api = MessageHandler::Api.new(self) + @store = {} + end + + def on_join(&block) + self.executor = block + end + + def on_open + stream.on_message joiner.send_hello + end + + def on_message(data) + handler = MessageHandler.resolve(data, self) + handler.handle + end + + def transmit(data) + stream.on_message data + end + end + end +end diff --git a/lib/wamp/connection/websocket_connection.rb b/lib/wamp/connection/websocket_connection.rb index 3e60c03..5411f89 100644 --- a/lib/wamp/connection/websocket_connection.rb +++ b/lib/wamp/connection/websocket_connection.rb @@ -12,17 +12,14 @@ def joined? module Wamp module Connection # Conn - class WebSocketConnection - attr_reader :url, :joiner, :session, :call_requests, :store, :websocket, :api - attr_accessor :executor + class WebSocketConnection < Session + attr_reader :url, :websocket def initialize(url = "ws://localhost:8080/ws", joiner = Wampproto::Joiner.new("realm1")) + super(joiner) @url = url - @joiner = joiner - @websocket = Wamp::Connection::WebsocketClient.new(self, protocols) - @session = Wampproto::Session.new(joiner.serializer) - @api = MessageHandler::Api.new(self) - @store = {} + @store = {} + @websocket = Wamp::Connection::WebsocketClient.new(self, protocols) end def run @@ -32,19 +29,8 @@ def run websocket.close end - def on_join(&block) - puts "blocking is saving" - self.executor = block - end - def on_open - data = joiner.send_hello - transmit data - end - - def on_message(data) - handler = MessageHandler.resolve(data, self) - handler.handle + transmit joiner.send_hello end def transmit(data) diff --git a/lib/wamp/message_handler/yield.rb b/lib/wamp/message_handler/yield.rb deleted file mode 100644 index 06d67d7..0000000 --- a/lib/wamp/message_handler/yield.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module Wamp - module MessageHandler - # Yield - class Yield < Base - def send_message(handler) - connection.transmit message - connection.call_requests[message.request_id] = handler - end - end - end -end