Created
March 24, 2020 06:16
-
-
Save tadman/bcbd560e507e0f0f5ddb25267b9e1775 to your computer and use it in GitHub Desktop.
Minimal Falcon + Rack + WebSocket Adapter Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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