Class: Hyperstack::Hotloader::Client
- Defined in:
- lib/hyperstack/hotloader/server.rb
Instance Attribute Summary collapse
-
#closed ⇒ Object
Returns the value of attribute closed.
-
#frame ⇒ Object
Returns the value of attribute frame.
-
#handshake ⇒ Object
Returns the value of attribute handshake.
-
#messaged ⇒ Object
Returns the value of attribute messaged.
-
#opened ⇒ Object
Returns the value of attribute opened.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #get_lazy_fiber ⇒ Object
-
#initialize(socket, handshake, server) ⇒ Client
constructor
A new instance of Client.
- #lazy_send(data) ⇒ Object
- #onclose(&blk) ⇒ Object
- #onmessage(&blk) ⇒ Object
- #onopen(&blk) ⇒ Object
- #send(data) ⇒ Object
- #send_some_lazy(count) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(socket, handshake, server) ⇒ Client
Returns a new instance of Client.
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/hyperstack/hotloader/server.rb', line 202 def initialize(socket, handshake, server) @socket = socket @handshake = handshake @frame = WebSocket::Frame::Incoming::Server.new(:version => @handshake.version) @opened = false @messaged = [] @lazy_queue = [] @lazy_current_queue = nil @closed = false @server = server end |
Instance Attribute Details
#closed ⇒ Object
Returns the value of attribute closed.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def closed @closed end |
#frame ⇒ Object
Returns the value of attribute frame.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def frame @frame end |
#handshake ⇒ Object
Returns the value of attribute handshake.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def handshake @handshake end |
#messaged ⇒ Object
Returns the value of attribute messaged.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def @messaged end |
#opened ⇒ Object
Returns the value of attribute opened.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def opened @opened end |
#socket ⇒ Object
Returns the value of attribute socket.
200 201 202 |
# File 'lib/hyperstack/hotloader/server.rb', line 200 def socket @socket end |
Instance Method Details
#get_lazy_fiber ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/hyperstack/hotloader/server.rb', line 232 def get_lazy_fiber # Create the fiber if needed if @lazy_fiber == nil or !@lazy_fiber.alive? @lazy_fiber = Fiber.new do @lazy_current_queue.each do |data| send(data) Fiber.yield unless @lazy_current_queue[-1] == data end end end return @lazy_fiber end |
#lazy_send(data) ⇒ Object
228 229 230 |
# File 'lib/hyperstack/hotloader/server.rb', line 228 def lazy_send(data) @lazy_queue.push data end |
#onclose(&blk) ⇒ Object
284 285 286 287 288 289 290 291 |
# File 'lib/hyperstack/hotloader/server.rb', line 284 def onclose(&blk) if @closed begin blk.call ensure end end end |
#onmessage(&blk) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/hyperstack/hotloader/server.rb', line 272 def (&blk) if @messaged.size > 0 begin @messaged.each do |x| blk.call(x.to_s) end ensure @messaged = [] end end end |
#onopen(&blk) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/hyperstack/hotloader/server.rb', line 262 def onopen(&blk) if @opened begin blk.call ensure @opened = false end end end |
#send(data) ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/hyperstack/hotloader/server.rb', line 218 def send(data) frame = WebSocket::Frame::Outgoing::Server.new(:version => @handshake.version, :data => data, :type => :text) begin @socket.write frame @socket.flush rescue @server.close(self) unless @closed end end |
#send_some_lazy(count) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/hyperstack/hotloader/server.rb', line 246 def send_some_lazy(count) # To save on cpu cycles, we don't want to be chopping and changing arrays, which could get quite large. Instead, # we iterate over an array which we are sure won't change out from underneath us. unless @lazy_current_queue @lazy_current_queue = @lazy_queue @lazy_queue = [] end completed = 0 begin get_lazy_fiber.resume completed += 1 end while (@lazy_queue.count > 0 or @lazy_current_queue.count > 0) and completed < count end |
#write(data) ⇒ Object
214 215 216 |
# File 'lib/hyperstack/hotloader/server.rb', line 214 def write(data) @socket.write data end |