-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebSockets #1305
base: main
Are you sure you want to change the base?
WebSockets #1305
Changes from all commits
58c9559
3244366
992b68c
53b3f2d
3f93318
07066d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,17 @@ module Lucky::Routable | |
end | ||
{% end %} | ||
|
||
# Define a route that responds to a WebSocket request | ||
macro ws(path, &block) | ||
{% unless path.starts_with?("/") %} | ||
{% path.raise "Path must start with a slash. Example: '/#{path}'" %} | ||
{% end %} | ||
|
||
add_route(:ws, {{ path }}, {{ @type.name.id }}) | ||
|
||
setup_ws_call_method(block) | ||
end | ||
|
||
# Define a route with a custom HTTP method. | ||
# | ||
# Use this method if you need to match a route with a custom HTTP method (verb). | ||
|
@@ -85,6 +96,35 @@ module Lucky::Routable | |
setup_call_method({{ yield }}) | ||
end | ||
|
||
# :nodoc: | ||
macro setup_ws_call_method(&block) | ||
|
||
abstract def on_message(message) | ||
abstract def on_close | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thought was by having these abstract, the websocket can proxy to them, and forces you to set these up. Websockets have a few other methods though.... are any of them as necessary? Is there ever a time you'd use a websocket and not use these? |
||
|
||
def call | ||
# Ensure clients_desired_format is cached by calling it | ||
clients_desired_format | ||
|
||
%pipe_result = run_before_pipes | ||
|
||
%response = if %pipe_result.is_a?(Lucky::Response) | ||
%pipe_result | ||
else | ||
{{ block.body }} | ||
send_text_response "", content_type: "plain/text" | ||
end | ||
|
||
%pipe_result = run_after_pipes | ||
|
||
if %pipe_result.is_a?(Lucky::Response) | ||
%pipe_result | ||
else | ||
%response | ||
end | ||
end | ||
end | ||
|
||
# :nodoc: | ||
macro setup_call_method(body) | ||
def call | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
abstract class Lucky::WebSocketAction < Lucky::Action | ||
@socket : HTTP::WebSocket? | ||
@handler : HTTP::WebSocketHandler | ||
|
||
def initialize(@context : HTTP::Server::Context, @route_params : Hash(String, String)) | ||
@handler = HTTP::WebSocketHandler.new do |ws| | ||
@socket = ws | ||
ws.on_ping { ws.pong("PONG") } | ||
call | ||
end | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would create a new handler for every websocket action you added. I'm not sure how many a person would add, but it seems like you should only have 1, and the handler would just route to each action based on where it connected. This looks like what Kemal seems to be doing too. A single instance that just adds each route and the handler. |
||
end | ||
|
||
def perform_websocket_action | ||
@handler.call(@context) | ||
end | ||
|
||
def socket : HTTP::WebSocket | ||
@socket.not_nil! | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Lucky | ||
class WebSocketRouteHandler | ||
include HTTP::Handler | ||
|
||
def call(context : HTTP::Server::Context) | ||
if websocket_upgrade_request?(context) | ||
handler = Lucky::Router.find_action(:ws, context.request.path) | ||
if handler | ||
Lucky::Log.dexter.debug { {handled_by: handler.payload.to_s} } | ||
handler.payload.new(context, handler.params).as(Lucky::WebSocketAction).perform_websocket_action | ||
else | ||
call_next(context) | ||
end | ||
else | ||
call_next(context) | ||
end | ||
end | ||
|
||
private def websocket_upgrade_request?(context) | ||
return unless upgrade = context.request.headers["Upgrade"]? | ||
return unless upgrade.compare("websocket", case_insensitive: true) == 0 | ||
|
||
context.request.headers.includes_word?("Connection", "Upgrade") | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would need
wss
too