Class: Hyperstack::Hotloader::Socket

Inherits:
Object
  • Object
show all
Includes:
IO::Writable, Native
Defined in:
lib/hyperstack/hotloader/socket.rb

Overview

Code taken from opal browser, did not want to force an opal-browser dependency

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

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_exec`d, otherwise it’s called with ‘self`



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hyperstack/hotloader/socket.rb', line 43

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.



69
# File 'lib/hyperstack/hotloader/socket.rb', line 69

alias_native :buffered, :bufferedAmount

#extensionsArray<String> (readonly)

Returns the extensions used by the socket.

Returns:

  • (Array<String>)

    the extensions used by the socket



110
111
112
# File 'lib/hyperstack/hotloader/socket.rb', line 110

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

#protocolString (readonly)

Returns the protocol of the socket.

Returns:

  • (String)

    the protocol of the socket



61
# File 'lib/hyperstack/hotloader/socket.rb', line 61

alias_native :protocol

#state:connecting, ... (readonly)

Returns the state of the socket.

Returns:

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

    the state of the socket



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyperstack/hotloader/socket.rb', line 90

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hyperstack/hotloader/socket.rb', line 73

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



65
# File 'lib/hyperstack/hotloader/socket.rb', line 65

alias_native :url

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/hyperstack/hotloader/socket.rb', line 15

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

Instance Method Details

#alive?Boolean

Check if the socket is alive.

Returns:

  • (Boolean)


115
116
117
# File 'lib/hyperstack/hotloader/socket.rb', line 115

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



130
131
132
# File 'lib/hyperstack/hotloader/socket.rb', line 130

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

#on(str, &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/hyperstack/hotloader/socket.rb', line 22

def on(str, &block)
  puts "putting #{str}"
  b =  block
  cmd = "foo.on#{str} = #{b}"
  puts cmd
  `#{cmd}`
  # `foo.on#{str} = #{b}`
end

#write(data) ⇒ Object

Send data to the socket.

Parameters:

  • data (#to_n)

    the data to send



122
123
124
# File 'lib/hyperstack/hotloader/socket.rb', line 122

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