Simple connection script to guide you to the start

TSP, 2007-10-21: This is a very simple script to help you get started with connecting to the IRC, or actually any socket based connection.

 set socket [socket -async irc.example.net 6667]
 proc recv {socket} {
         gets $socket line
         # process $line..
 }
 fconfigure $socket -buffering line -translation crlf
 fileevent $socket readable "recv $socket"

Be sure to change "irc.example.net" (and "6667" if appropriate). recv will be called when the client gets a line of data from the server.

PT: you should hook up the writable fileevent. If you are unable to make a connection with an asynchronous socket then you will get notification of this in the writable fileevent. So this is better done as:

 proc Write {chan} {
     fileevent $chan writable {}
     if {[set err [fconfigure $chan -error]] ne ""} {
         puts stderr "failed to open socket: $err"
         close $chan
         return
     }
     fconfigure $chan -blocking 0 -buffering line -translation crlf -encoding utf-8
     fileevent $chan readable [list Read $chan]
     puts $chan "Initial protocol connection string (if any)"
 }
 proc Read {chan} {
     ... read handling ...
 }
 set socket [socket -async $server $port]
 fileevent $socket writable [list Write $socket]
 vwait forever

See also picoIRC 0.2 or minibot


Category Example Category Internet