Class: Whoosh::Streaming::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/streaming/websocket.rb

Defined Under Namespace

Classes: AsyncWSWrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ WebSocket

Returns a new instance of WebSocket.



10
11
12
13
14
15
16
17
# File 'lib/whoosh/streaming/websocket.rb', line 10

def initialize(env)
  @env = env
  @ws = nil
  @closed = false
  @on_open = nil
  @on_message = nil
  @on_close = nil
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/whoosh/streaming/websocket.rb', line 8

def env
  @env
end

Class Method Details

.websocket?(env) ⇒ Boolean

Check if the request is a WebSocket upgrade

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/whoosh/streaming/websocket.rb', line 20

def self.websocket?(env)
  upgrade = env["HTTP_UPGRADE"]
  upgrade && upgrade.downcase == "websocket"
end

Instance Method Details

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



45
46
47
48
49
50
51
# File 'lib/whoosh/streaming/websocket.rb', line 45

def close(code = nil, reason = nil)
  return if @closed
  @closed = true
  @ws&.close(code || 1000, reason || "")
rescue
  # Already closed
end

#closed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/whoosh/streaming/websocket.rb', line 53

def closed?
  @closed
end

#on_close(&block) ⇒ Object



34
35
36
# File 'lib/whoosh/streaming/websocket.rb', line 34

def on_close(&block)
  @on_close = block
end

#on_message(&block) ⇒ Object



30
31
32
# File 'lib/whoosh/streaming/websocket.rb', line 30

def on_message(&block)
  @on_message = block
end

#on_open(&block) ⇒ Object

Register callbacks



26
27
28
# File 'lib/whoosh/streaming/websocket.rb', line 26

def on_open(&block)
  @on_open = block
end

#rack_responseObject

Returns a Rack response — auto-detects Faye (Puma) or Async (Falcon)



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/whoosh/streaming/websocket.rb', line 58

def rack_response
  unless self.class.websocket?(@env)
    return [400, { "content-type" => "text/plain" }, ["Not a WebSocket request"]]
  end

  if async_websocket_available? && falcon_env?
    rack_response_async
  else
    rack_response_faye
  end
end

#send(data) ⇒ Object

Send data to the client



39
40
41
42
43
# File 'lib/whoosh/streaming/websocket.rb', line 39

def send(data)
  return if @closed || @ws.nil?
  formatted = data.is_a?(String) ? data : JSON.generate(data)
  @ws.send(formatted)
end

#trigger_close(code = 1000, reason = "") ⇒ Object



75
76
77
78
# File 'lib/whoosh/streaming/websocket.rb', line 75

def trigger_close(code = 1000, reason = "")
  @on_close&.call(code, reason)
  @closed = true
end

#trigger_message(msg) ⇒ Object

For testing without real socket



71
72
73
# File 'lib/whoosh/streaming/websocket.rb', line 71

def trigger_message(msg)
  @on_message&.call(msg)
end

#trigger_openObject



80
81
82
# File 'lib/whoosh/streaming/websocket.rb', line 80

def trigger_open
  @on_open&.call
end