~aleteoryx/tclfeed-bsky

0bcbdf7628616649161906320b3157e40d075014 — Aleteoryx 27 days ago 7582957
stop websocket dying during database stuff
1 files changed, 46 insertions(+), 42 deletions(-)

M src/ws.tcl
M src/ws.tcl => src/ws.tcl +46 -42
@@ 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"
    }
  }
}