Class: Async::WebSocket::Server

Inherits:
Connection show all
Defined in:
lib/async/websocket/server.rb

Constant Summary collapse

HIJACK_RESPONSE =
[-1, {}, []].freeze

Constants inherited from Connection

Connection::BLOCK_SIZE, Connection::EVENTS

Instance Attribute Summary collapse

Attributes inherited from Connection

#driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#close, #next_event, #next_message, #send_message, #send_text, #write

Constructor Details

#initialize(env, socket, **options) ⇒ Server

Returns a new instance of Server.



26
27
28
29
30
31
32
33
# File 'lib/async/websocket/server.rb', line 26

def initialize(env, socket, **options)
  scheme = env['rack.url_scheme'] == 'https' ? 'wss' : 'ws'
  
  @url = "#{scheme}://#{env['HTTP_HOST']}#{env['REQUEST_URI']}"
  @env = env
  
  super(socket, ::WebSocket::Driver.rack(self, options))
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



35
36
37
# File 'lib/async/websocket/server.rb', line 35

def env
  @env
end

#urlObject (readonly)

Returns the value of attribute url.



36
37
38
# File 'lib/async/websocket/server.rb', line 36

def url
  @url
end

Class Method Details

.open(env, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/async/websocket/server.rb', line 40

def self.open(env, **options)
  if ::WebSocket::Driver.websocket?(env)
    return nil unless env['rack.hijack?']
    
    # https://github.com/rack/rack/blob/master/SPEC#L89-L93
    peer = Async::IO.try_convert(
      env['rack.hijack'].call
    )
    
    connection = self.new(env, peer, options)
    
    return connection unless block_given?
    
    begin
      yield(connection)
      
      return HIJACK_RESPONSE
    ensure
      connection.close
      peer.close
    end
  end
end