Class: Hyperstack::Hotloader::Socket
- 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
-
#buffered ⇒ Integer
readonly
The amount of buffered data.
-
#extensions ⇒ Array<String>
readonly
The extensions used by the socket.
-
#protocol ⇒ String
readonly
The protocol of the socket.
-
#state ⇒ :connecting, ...
readonly
The state of the socket.
-
#type ⇒ :blob, ...
readonly
The type of the socket.
-
#url ⇒ String
readonly
The URL the socket is connected to.
Class Method Summary collapse
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check if the socket is alive.
-
#close(code = nil, reason = nil) ⇒ Object
Close the socket.
-
#initialize(url, protocol = nil) { ... } ⇒ Socket
constructor
Create a connection to the given URL, optionally using the given protocol.
- #on(str, &block) ⇒ Object
-
#write(data) ⇒ Object
Send data to the socket.
Constructor Details
#initialize(url, protocol = nil) { ... } ⇒ Socket
Create a connection to the given URL, optionally using the given protocol.
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
#buffered ⇒ Integer (readonly)
Returns the amount of buffered data.
69 |
# File 'lib/hyperstack/hotloader/socket.rb', line 69 alias_native :buffered, :bufferedAmount |
#extensions ⇒ Array<String> (readonly)
Returns 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 |
#protocol ⇒ String (readonly)
Returns 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.
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.
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 |
#url ⇒ String (readonly)
Returns the URL the socket is connected to.
65 |
# File 'lib/hyperstack/hotloader/socket.rb', line 65 alias_native :url |
Class Method Details
.supported? ⇒ 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.
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.
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.
122 123 124 |
# File 'lib/hyperstack/hotloader/socket.rb', line 122 def write(data) `#@native.send(#{data.to_n})` end |