~aleteoryx/tclfeed-bsky

7582957d6d9620490936466c378b318af5f95bd2 — Aleteoryx 27 days ago 192de7f
hopefully make jetstream code a little less silent-failure-prone
2 files changed, 13 insertions(+), 8 deletions(-)

M src/jetstream.tcl
M src/ws.tcl
M src/jetstream.tcl => src/jetstream.tcl +1 -0
@@ 15,6 15,7 @@ namespace eval ::jetstream {
      switch -- $mode {
        close {
          # cursed
          puts "websocket $sock closed! reconnecting..."
          ::jetstream::on_ws_${postfix}_close
        }


M src/ws.tcl => src/ws.tcl +12 -8
@@ 2,10 2,9 @@ namespace eval ::ws {
  proc read_frame {sock} {
    fconfigure $sock -blocking 1

    if ![binary scan [read $sock 2] cc byte1 byte2] {
      close $sock
      return {}
    }
    set status [binary scan [read $sock 2] cc byte1 byte2]
    if {!$status} { close $sock; return }

    set byte1 [expr {$byte1 & 0xFF}]
    set byte2 [expr {$byte2 & 0xFF}]



@@ 19,14 18,17 @@ namespace eval ::ws {
    set p_len [expr {$byte2 & 0x7F}]

    if {$p_len == 126} {
      binary scan [read $sock 2] S p_len
      set status [binary scan [read $sock 2] S p_len]
      set p_len [expr {$p_len & 0xFFFF}]
      if {!$status} { close $sock; return }
    } elseif {$p_len == 127} {
      binary scan [read $sock 8] W p_len
      set status [binary scan [read $sock 8] W p_len]
      if {!$status} { close $sock; return }
    }

    if {$p_mask} {
      binary scan [read $sock 4] c4 p_mask_key
      set status [binary scan [read $sock 4] c4 p_mask_key]
      if {!$status} { close $sock; return }
      set p_mask_bytes
      foreach byte $p_mask_key {
        lappend p_mask_bytes [expr {$byte & 0xFF}]


@@ 34,6 36,7 @@ namespace eval ::ws {
    }

    set data [read $sock $p_len]
    if {[string length $data] != $p_len} { close $sock; return }
    fconfigure $sock -blocking 0

    if {$p_mask} {


@@ 53,7 56,7 @@ namespace eval ::ws {
    set frame {}

    if {$opcode > 16 || $opcode < 0} {
      return -code error "Opcode $opcode invalid. Must be in [0,16]."
      return -code error "Opcode $opcode invalid. Must be in \[0,16\]."
    }

    append frame [binary format cc \


@@ 85,6 88,7 @@ namespace eval ::ws {
    puts -nonewline $sock $frame
  }
  proc int-doping {sock} {
    if {[chan names $sock] == $sock} return
    ping $sock
    after 10000 [list ::ws::int-doping $sock]
  }