Class: Browser::Socket

Inherits:
IO show all
Includes:
Event::Target, IO::Writable, Native::Wrapper
Defined in:
opal/browser/socket.rb

Overview

A Socket allows the browser and a server to have a bidirectional data connection.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Event::Target

#off, #on, #on!, #one, #trigger, #trigger!

Constructor Details

#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

Instance Attribute Details

#bufferedInteger (readonly)

Returns the amount of buffered data.

Returns:

  • (Integer)

    the amount of buffered data.



54
# File 'opal/browser/socket.rb', line 54

alias_native :buffered, :bufferedAmount

#extensionsArray<String> (readonly)

Returns the extensions used by the socket.

Returns:

  • (Array<String>)

    the extensions used by the socket



95
96
97
# File 'opal/browser/socket.rb', line 95

def extensions
  `#@native.extensions`.split(/\s*,\s*/)
end

#protocolString (readonly)

Returns the protocol of the socket.

Returns:

  • (String)

    the protocol of the socket



46
# File 'opal/browser/socket.rb', line 46

alias_native :protocol

#state:connecting, ... (readonly)

Returns the state of the socket.

Returns:

  • (:connecting, :open, :closing, :closed)

    the state of the socket



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'opal/browser/socket.rb', line 75

def state
  %x{
    switch (#@native.readyState) {
      case window.WebSocket.CONNECTING:
        return "connecting";

      case window.WebSocket.OPEN:
        return "open";

      case window.WebSocket.CLOSING:
        return "closing";

      case window.WebSocket.CLOSED:
        return "closed";
    }
  }
end

#type:blob, ... (readonly)

Returns the type of the socket.

Returns:

  • (:blob, :buffer, :string)

    the type of the socket



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'opal/browser/socket.rb', line 58

def type
  %x{
    switch (#@native.binaryType) {
      case "blob":
        return "blob";

      case "arraybuffer":
        return "buffer";

      default:
        return "string";
    }
  }
end

#urlString (readonly)

Returns the URL the socket is connected to.

Returns:

  • (String)

    the URL the socket is connected to



50
# File 'opal/browser/socket.rb', line 50

alias_native :url

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'opal/browser/socket.rb', line 9

def self.supported?
  Browser.supports? :WebSocket
end

Instance Method Details

#alive?Boolean

Check if the socket is alive.

Returns:

  • (Boolean)


100
101
102
# File 'opal/browser/socket.rb', line 100

def alive?
  state == :open
end

#close(code = nil, reason = nil) ⇒ Object

Close the socket.

Parameters:

  • code (Integer, nil) (defaults to: nil)

    the error code

  • reason (String, nil) (defaults to: nil)

    the reason for closing



119
120
121
# File 'opal/browser/socket.rb', line 119

def close(code = nil, reason = nil)
  `#@native.close(#{code.to_n}, #{reason.to_n})`
end

#write(data) ⇒ Object Also known as: <<, send

Send data to the socket.

Parameters:

  • data (#to_n)

    the data to send



107
108
109
# File 'opal/browser/socket.rb', line 107

def write(data)
  `#@native.send(#{data.to_n})`
end