Class: WebSocket

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

Defined Under Namespace

Classes: MessageEvent

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ WebSocket

Returns a new instance of WebSocket.



27
28
29
30
31
# File 'lib/opal/drb/websocket.rb', line 27

def initialize(url)
  super `new WebSocket(url)`
  `self.native.binaryType = 'arraybuffer'`
  @listeners = {}
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/opal/drb/websocket.rb', line 67

def closed?
  `#@native.readyState === 3`
end

#closing?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/opal/drb/websocket.rb', line 63

def closing?
  `#@native.readyState === 2`
end

#connecting?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/opal/drb/websocket.rb', line 55

def connecting?
  `#@native.readyState === 0`
end

#off(handler) ⇒ Object



51
52
53
# File 'lib/opal/drb/websocket.rb', line 51

def off handler
  remove_event_listener(*@listeners[handler])
end

#onclose(&block) ⇒ Object



45
46
47
48
49
# File 'lib/opal/drb/websocket.rb', line 45

def onclose(&block)
  listener = Proc.new {|event| yield Native(event) }
  @listeners[block] = [:close, listener]
  add_event_listener('close', &listener)
end

#onmessage(&block) ⇒ Object



33
34
35
36
37
# File 'lib/opal/drb/websocket.rb', line 33

def onmessage(&block)
  listener = Proc.new {|event| yield MessageEvent.new(event) if self.open? }
  @listeners[block] = [:message, listener]
  add_event_listener('message', &listener)
end

#onopen(&block) ⇒ Object



39
40
41
42
43
# File 'lib/opal/drb/websocket.rb', line 39

def onopen(&block)
  listener = Proc.new {|event| yield Native(event) }
  @listeners[block] = [:open, listener]
  add_event_listener('open', &listener)
end

#open?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/opal/drb/websocket.rb', line 59

def open?
  `#@native.readyState === 1`
end