Method: Browser::Socket#initialize

Defined in:
opal/browser/socket.rb

#initialize(url, protocol = nil) { ... } ⇒ Socket

Create a connection to the given URL, optionally using the given protocol.

Parameters:

  • url (String)

    the URL to connect to

  • protocol (String) (defaults to: nil)

    the protocol to use

Yields:

  • if the block has no parameters it's instance_execd, otherwise it's called with self



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'opal/browser/socket.rb', line 28

def initialize(url, protocol = nil, &block)
  if native?(url)
    super(url)
  elsif protocol
    super(`new window.WebSocket(#{url.to_s}, #{protocol.to_n})`)
  else
    super(`new window.WebSocket(#{url.to_s})`)
  end

  if block.arity == 0
    instance_exec(&block)
  else
    block.call(self)
  end if block
end