Skip to content

Commit

Permalink
Update websocket.cr
Browse files Browse the repository at this point in the history
  • Loading branch information
grkek authored Dec 26, 2024
1 parent 53d1e24 commit 4f286c7
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/grip/controllers/websocket.cr
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
module Grip
module Controllers
class WebSocket
class WebSocket < HTTP::WebSocket
alias Context = HTTP::Server::Context
alias Socket = HTTP::WebSocket::Protocol

include HTTP::Handler
include Helpers::Singleton

getter? closed = false

# :nodoc:
def initialize
@buffer = Bytes.new(4096)
@current_message = IO::Memory.new
@ws = HTTP::WebSocket::Protocol.new(IO::Memory.new, sync_close: true)
end

def on_open(context : Context, socket : Socket) : Void
Expand All @@ -33,10 +32,6 @@ module Grip
def on_close(context : Context, socket : Socket, close_code : HTTP::WebSocket::CloseCode | Int?, message : String) : Void
end

protected def check_open
raise IO::Error.new "Closed socket" if closed?
end

def run(context, socket)
on_open(context, socket)

Expand Down Expand Up @@ -125,9 +120,10 @@ module Grip
response.headers["Upgrade"] = "websocket"
response.headers["Connection"] = "Upgrade"
response.headers["Sec-WebSocket-Accept"] = accept_code

response.upgrade do |io|
socket = HTTP::WebSocket::Protocol.new(io, sync_close: true)
run(context, socket)
@ws = HTTP::WebSocket::Protocol.new(io, sync_close: true)
run(context, @ws)
end

context
Expand Down

0 comments on commit 4f286c7

Please sign in to comment.