jsf
jsf

Reputation: 943

tcllib websocket: How to print received messages to a text widget

Here is the important part of my code:

set message_initialization_result ""

proc logMessage {msg} {
    upvar outputtext outputtext

    if { [string length $outputtext] > 0 } {
        if { [string length $msg] > 0 } {
            $outputtext insert end $msg
            $outputtext insert end "\n\n"
        }
    }
}

proc handler { sock type msg } {
    upvar outputtext outputtext
    upvar message_initialization_result message_initialization_result

    if {[catch {
    switch -glob -nocase -- $type {
    co* {
        logMessage "Connected on $sock"
    }
    te* {
        logMessage "Received:\n$msg"
    }
    cl* -
    dis* {
        logMessage "Disconnected"
    }
    }
    }]} {
        set message_initialization_result "$message_initialization_result handler"
    }
}

set sock [::websocket::open ws://127.0.0.1:10010 handler]

...
logMessage $message_initialization_result

outputtext is a variable that holds a text widget.

How do I get the handler to print the messages it receives to that text widget? And how can I check if this handler is called at all? I tried provoking errors but it seems that they are all caught by a catch inside the websocket library.

Upvotes: 0

Views: 144

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13282

I'm collecting a couple of points for posterity...

  • For quick-and-dirty debugging, add the command puts [info level 0] to the beginning of those procedures you want to track. In this way you get to see every time the procedure is called, and what arguments it received.
  • If you don't have a console in a Tk-based script (for instance on Windows) and thus can't see the output from puts, try opening the Tk console with console show.
  • If you can't get output as above, try sending output to a file. If you can use fileutil, the command ::fileutil::appendToFile log.txt $msg is perfect for light logging. Check out the options to the command.
  • No fileutil? Try set f [open log.txt a] ; puts $f $msg ; close $f. Opening and closing the file every time keeps it flushed and means you don't need as much of intrusive injections and synchronizations.

Documentation: close, console, fileutil (package), info, open, puts, set

Upvotes: 1

Related Questions