From 0bcbdf7628616649161906320b3157e40d075014 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Sun, 24 Nov 2024 21:02:14 -0500 Subject: [PATCH] stop websocket dying during database stuff --- src/ws.tcl | 88 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/src/ws.tcl b/src/ws.tcl index 854f296..d1d8bcc 100644 --- a/src/ws.tcl +++ b/src/ws.tcl @@ -220,54 +220,58 @@ namespace eval ::ws::c { [set callback($sock)] $sock close [dict get $frame data] } - switch -- [dict get $frame opcode] { - 0 { - if {[set mode($sock)] ni {1 2}} { - ${log}::error "Got a continuation frame with no starting frame on $sock! Closing!" + if {[catch { + switch -- [dict get $frame opcode] { + 0 { + if {[set mode($sock)] ni {1 2}} { + ${log}::error "Got a continuation frame with no starting frame on $sock! Closing!" + close $sock + [set callback($sock)] $sock close [set frag($sock)] + } + append frags($sock) [dict get $frame data] + if {[dict get $frame fin]} { + set frags($sock) {} + } + } + 1 { + if {[dict get $frame fin]} { + set mode($sock) {} + set frag($sock) {} + [set callback($sock)] $sock text [dict get $frame data] + } else { + set mode($sock) text + set frag($sock) [dict get $frame data] + } + } + 2 { + if {[dict get $frame fin]} { + set mode($sock) {} + set frag($sock) {} + [set callback($sock)] $sock binary [dict get $frame data] + } else { + set mode($sock) binary + set frag($sock) [dict get $frame data] + } + } + + 8 { close $sock + [set callback($sock)] $sock close [dict get $frame data] } - append frags($sock) [dict get $frame data] - if {[dict get $frame fin]} { - [set callback($sock)] $sock [set mode($sock)] [set frag($sock)] - set frags($sock) {} + 9 { + ::ws::send_frame 0 10 [dict get $frame data] [dict get $frame len] 1 } - } - 1 { - if {[dict get $frame fin]} { - set mode($sock) {} - set frag($sock) {} - [set callback($sock)] $sock text [dict get $frame data] - } else { - set mode($sock) text - set frag($sock) [dict get $frame data] + 10 { + # TODO: kill connection on missed ping? but jetstream doesn't send any it looks like } - } - 2 { - if {[dict get $frame fin]} { - set mode($sock) {} - set frag($sock) {} - [set callback($sock)] $sock binary [dict get $frame data] - } else { - set mode($sock) binary - set frag($sock) [dict get $frame data] + default { + ${log}::error "Unknown opcode [dict get $frame opcode] on $sock! Closing!" + close $sock + [set callback($sock)] $sock close [dict get $frame data] } } - - 8 { - close $sock - [set callback($sock)] $sock close [dict get $frame data] - } - 9 { - ::ws::send_frame 0 10 [dict get $frame data] [dict get $frame len] 1 - } - 10 { - # TODO: kill connection on missed ping? but jetstream doesn't send any it looks like - } - default { - ${log}::error "Unknown opcode [dict get $frame opcode] on $sock! Closing!" - close $sock - [set callback($sock)] $sock close [dict get $frame data] - } + } return] { + ${log}::error "error in websocket callback for ${sock}: $result" } } } -- 2.45.2