Skip to content

Instantly share code, notes, and snippets.

@tadman
Created March 24, 2020 06:16
Show Gist options
  • Save tadman/bcbd560e507e0f0f5ddb25267b9e1775 to your computer and use it in GitHub Desktop.
Save tadman/bcbd560e507e0f0f5ddb25267b9e1775 to your computer and use it in GitHub Desktop.
Minimal Falcon + Rack + WebSocket Adapter Demo
require 'async'
require 'async/http/endpoint'
require 'async/websocket/adapters/rack'
require 'falcon'
websocket_endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000')
module WebSocketApp
def self.call(env)
Async::WebSocket::Adapters::Rack.open(env, protocols: %w[ ws ]) do |connection|
while (message = connection.read)
# ...
end
end or [ 200, { }, [ "Websocket only." ] ]
end
end
app = Falcon::Server.middleware(WebSocketApp)
Async do
server = Falcon::Server.new(app, websocket_endpoint)
server.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment