Class: Protocol::WebSocket::Extensions::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/extensions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions) ⇒ Server

Returns a new instance of Server.



84
85
86
87
# File 'lib/protocol/websocket/extensions.rb', line 84

def initialize(extensions)
  @extensions = extensions
  @accepted = []
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



90
91
92
# File 'lib/protocol/websocket/extensions.rb', line 90

def accepted
  @accepted
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



89
90
91
# File 'lib/protocol/websocket/extensions.rb', line 89

def extensions
  @extensions
end

Class Method Details

.defaultObject



78
79
80
81
82
# File 'lib/protocol/websocket/extensions.rb', line 78

def self.default
  self.new([
    [Extension::Compression, {}]
  ])
end

Instance Method Details

#accept(headers) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/protocol/websocket/extensions.rb', line 98

def accept(headers)
  extensions = []
  
  named = self.named
  response = []
  
  # Each response header should map to at least one extension.
  Extensions.parse(headers) do |name, arguments|
    if extension = named[name]
      klass, options = extension
      
      if result = klass.negotiate(arguments, **options)
        header, options = result
        
        # The extension is accepted and no further offers will be considered:
        named.delete(name)
        
        yield header
        
        @accepted << [klass, options]
      end
    end
  end
  
  return headers
end

#apply(connection) ⇒ Object



125
126
127
128
129
# File 'lib/protocol/websocket/extensions.rb', line 125

def apply(connection)
  @accepted.reverse_each do |(klass, options)|
    klass.server(connection, **options)
  end
end

#namedObject



92
93
94
95
96
# File 'lib/protocol/websocket/extensions.rb', line 92

def named
  @extensions.map do |extension|
    [extension.first::NAME, extension]
  end.to_h
end