Class: WampClient::Transport::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wamp_client/transport/base.rb

Direct Known Subclasses

EventMachineBase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Constructor for the transport

Parameters:

  • options (Hash)

    The connection options. the different options are as follows

Options Hash (options):

  • :uri (String)

    The url to connect to

  • :proxy (String)

    The proxy to use

  • :protocol (String)

    The protocol

  • :headers (Hash)

    Custom headers to include during the connection

  • :serializer (WampClient::Serializer::Base)

    The serializer to use



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wamp_client/transport/base.rb', line 86

def initialize(options)

  # Initialize the parameters
  self.connected = false
  self.uri = options[:uri]
  self.proxy = options[:proxy]
  self.headers = options[:headers] || {}
  self.protocol = options[:protocol] || 'wamp.2.json'
  self.serializer = options[:serializer] || WampClient::Serializer::JSONSerializer.new

  # Add the wamp.2.json protocol header
  self.headers['Sec-WebSocket-Protocol'] = self.protocol

  # Initialize callbacks
  @on_open = nil
  @on_close = nil
  @on_message = nil
  @on_error = nil

end

Instance Attribute Details

#connectedObject

Returns the value of attribute connected.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def connected
  @connected
end

#headersObject

Returns the value of attribute headers.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def protocol
  @protocol
end

#proxyObject

Returns the value of attribute proxy.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def proxy
  @proxy
end

#serializerObject

Returns the value of attribute serializer.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def serializer
  @serializer
end

#uriObject

Returns the value of attribute uri.



77
78
79
# File 'lib/wamp_client/transport/base.rb', line 77

def uri
  @uri
end

Class Method Details

.add_timer(milliseconds, &callback) ⇒ Object

Process the callback when the timer expires

Parameters:

  • milliseconds (Integer)
    • The number

  • callback (block)
    • The callback that is fired when the timer expires



131
132
133
# File 'lib/wamp_client/transport/base.rb', line 131

def self.add_timer(milliseconds, &callback)
  # Implement in subclass
end

.start_event_machine(&block) ⇒ Object

Method to start the event machine for the socket



139
140
141
# File 'lib/wamp_client/transport/base.rb', line 139

def self.start_event_machine(&block)
  # Implement in subclass
end

.stop_event_machineObject

Method to stop the vent machine



144
145
146
# File 'lib/wamp_client/transport/base.rb', line 144

def self.stop_event_machine
  # Implement in subclass
end

Instance Method Details

#add_timer(milliseconds, &callback) ⇒ Object

Implement in subclass



134
135
136
# File 'lib/wamp_client/transport/base.rb', line 134

def add_timer(milliseconds, &callback)
  self.class.add_timer(milliseconds, &callback)
end

#connectObject

Connects to the WAMP Server using the transport



108
109
110
# File 'lib/wamp_client/transport/base.rb', line 108

def connect
  # Implement in subclass
end

#connected?Boolean

Returns true if the transport it connected

Returns:

  • (Boolean)


118
119
120
# File 'lib/wamp_client/transport/base.rb', line 118

def connected?
  self.connected
end

#disconnectObject

Disconnects from the WAMP Server



113
114
115
# File 'lib/wamp_client/transport/base.rb', line 113

def disconnect
  # Implement in subclass
end

#on(event, &callback) ⇒ Object

Simple setter for callbacks



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wamp_client/transport/base.rb', line 62

def on(event, &callback)
  case event
    when :open
      self.on_open(&callback)
    when :close
      self.on_close(&callback)
    when :message
      self.on_message(&callback)
    when :error
      self.on_error(&callback)
    else
      raise RuntimeError, "Unknown on(event) '#{event}'"
  end
end

#on_close(&on_close) ⇒ Object



43
44
45
# File 'lib/wamp_client/transport/base.rb', line 43

def on_close(&on_close)
  @on_close = on_close
end

#on_error(&on_error) ⇒ Object



57
58
59
# File 'lib/wamp_client/transport/base.rb', line 57

def on_error(&on_error)
  @on_error = on_error
end

#on_message(&on_message) ⇒ Object



50
51
52
# File 'lib/wamp_client/transport/base.rb', line 50

def on_message(&on_message)
  @on_message = on_message
end

#on_open(&on_open) ⇒ Object



36
37
38
# File 'lib/wamp_client/transport/base.rb', line 36

def on_open(&on_open)
  @on_open = on_open
end

#send_message(msg) ⇒ Object

Sends a Message

Parameters:

  • msg (Array)
    • The message payload to send



124
125
126
# File 'lib/wamp_client/transport/base.rb', line 124

def send_message(msg)
  # Implement in subclass
end