Class: Async::WebSocket::Client
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Async::WebSocket::Client
- Includes:
- Protocol::WebSocket::Headers
- Defined in:
- lib/async/websocket/client.rb
Overview
This is a basic synchronous websocket client:
Defined Under Namespace
Classes: ClientCloseDecorator, Framer
Class Method Summary collapse
-
.connect(endpoint, *arguments, **options, &block) ⇒ Connection
An open websocket connection to the given endpoint.
-
.open(endpoint, **options, &block) ⇒ Client
A client which can be used to establish websocket connections to the given endpoint.
Instance Method Summary collapse
- #connect(authority, path, scheme: @delegate.scheme, headers: nil, handler: Connection, extensions: ::Protocol::WebSocket::Extensions::Client.default, **options, &block) ⇒ Object
-
#initialize(client, **options) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(client, **options) ⇒ Client
Returns a new instance of Client.
73 74 75 76 77 |
# File 'lib/async/websocket/client.rb', line 73 def initialize(client, **) super(client) = end |
Class Method Details
.connect(endpoint, *arguments, **options, &block) ⇒ Connection
Returns an open websocket connection to the given endpoint.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/async/websocket/client.rb', line 56 def self.connect(endpoint, *arguments, **, &block) Sync do client = self.open(endpoint, *arguments) connection = client.connect(endpoint., endpoint.path, **) return ClientCloseDecorator.new(client, connection) unless block_given? begin yield connection ensure connection&.close client&.close end end end |
.open(endpoint, **options, &block) ⇒ Client
Returns a client which can be used to establish websocket connections to the given endpoint.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/async/websocket/client.rb', line 27 def self.open(endpoint, **, &block) client = self.new(HTTP::Client.new(endpoint, **), mask: true) return client unless block_given? begin yield client ensure client.close end end |
Instance Method Details
#connect(authority, path, scheme: @delegate.scheme, headers: nil, handler: Connection, extensions: ::Protocol::WebSocket::Extensions::Client.default, **options, &block) ⇒ Object
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 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/async/websocket/client.rb', line 99 def connect(, path, scheme: @delegate.scheme, headers: nil, handler: Connection, extensions: ::Protocol::WebSocket::Extensions::Client.default, **, &block) headers = ::Protocol::HTTP::Headers[headers] extensions&.offer do |extension| headers.add(SEC_WEBSOCKET_EXTENSIONS, extension.join("; ")) end request = Request.new(scheme, , path, headers, **) pool = @delegate.pool connection = pool.acquire response = request.call(connection) unless response.stream? raise ConnectionError.new("Failed to negotiate connection!", response.unwrap) end protocol = response.headers[SEC_WEBSOCKET_PROTOCOL]&.first stream = response.stream framer = Framer.new(pool, connection, stream) connection = nil if extension_headers = response.headers[SEC_WEBSOCKET_EXTENSIONS] extensions.accept(extension_headers) end response = nil stream = nil return handler.call(framer, protocol, extensions, **, &block) ensure pool.release(connection) if connection end |