Class: DRb::WebSocket::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/drb/websocket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, config) ⇒ Server

Returns a new instance of Server.



181
182
183
184
185
# File 'lib/opal/drb/websocket.rb', line 181

def initialize(uri, config)
  @uri = "#{uri}/#{SecureRandom.uuid}"
  @config = config
  reconnect
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



179
180
181
# File 'lib/opal/drb/websocket.rb', line 179

def uri
  @uri
end

Instance Method Details

#accept(&block) ⇒ Object



214
215
216
# File 'lib/opal/drb/websocket.rb', line 214

def accept(&block)
  @accepter = block
end

#closeObject



187
188
189
# File 'lib/opal/drb/websocket.rb', line 187

def close
  @ws.close
end

#reconnectObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/opal/drb/websocket.rb', line 191

def reconnect
  @ws.close if @ws

  @ws = ::WebSocket.new(@uri)

  @ws.onclose do |event|
    reconnect
  end

  @ws.onmessage do |event|
    message_data = event.data.to_s
    sender_id = message_data.slice(0, 36)
    message = message_data.slice(36, message_data.length - 36)
    stream = StrStream.new(message)
    server_side = ServerSide.new(stream, @config, uri)
    @accepter.call server_side

    send_data = sender_id.bytes.each_slice(2).map(&:first)
    send_data += server_side.reply.bytes.each_slice(2).map(&:first)
    @ws.send(`new Uint8Array(#{send_data}).buffer`)
  end
end