mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Adding websocket support :)
This commit is contained in:
parent
0cc66cc9de
commit
06ced7790b
4 changed files with 83 additions and 0 deletions
|
@ -4,6 +4,7 @@ module Kemal
|
|||
class Config
|
||||
INSTANCE = Config.new
|
||||
HANDLERS = [] of HTTP::Handler
|
||||
WS_HANDLERS = [] of HTTP::Handler
|
||||
property ssl, port, env, workers, public_folder
|
||||
|
||||
def initialize
|
||||
|
@ -26,6 +27,10 @@ module Kemal
|
|||
HANDLERS << handler
|
||||
end
|
||||
|
||||
def add_ws_handler(handler : HTTP::WebSocketHandler)
|
||||
HANDLERS << handler
|
||||
end
|
||||
|
||||
# Reads configuration from config.yml. Currently it only supports the public_folder
|
||||
# option.
|
||||
# config.yml
|
||||
|
|
|
@ -5,3 +5,7 @@ HTTP_METHODS = %w(get post put patch delete)
|
|||
Kemal::Handler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||
end
|
||||
{% end %}
|
||||
|
||||
def ws(path, &block : HTTP::WebSocketHandler::WebSocketSession -> _)
|
||||
Kemal::WebsocketHandler.new path, &block
|
||||
end
|
||||
|
|
13
src/kemal/websocket_handler.cr
Normal file
13
src/kemal/websocket_handler.cr
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Kemal::WebsocketHandler < HTTP::WebSocketHandler
|
||||
getter handler
|
||||
|
||||
def initialize(@path, &@proc : WebSocketSession ->)
|
||||
@handler = @proc
|
||||
Kemal.config.add_ws_handler self
|
||||
end
|
||||
|
||||
def call(request)
|
||||
return call_next(request) unless request.path.not_nil! == @path
|
||||
super
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue