NaviServer - programmable web server

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

# ns_sockcallback(n) 4.99.8 naviserver "NaviServer Built-in Commands"

## Name

ns_sockcallback - Register a socket callback script

## Table Of Contents

## Synopsis

- ns_sockcallback sockid script when ?timeout?

## Description

ns_sockcallback registers a socket callback script.

## COMMANDS

ns_sockcallback sockid script when ?timeout?

  The optional argument when can consist of one or more characters of r, w, e, or x, specifying, when the callback should fire. The optional argument timeout can be of the form secs:?microsecs?. When the callback is fired, the script will be called with additional sockid and when. The value of when will be as follows: Value of, when Condition under which the script will be called:

  - r - the socket is readable

  - w - the socket is writeable

  - e - the socket has an exceptional condition

  - x - the server is shutting down

  - t - timeout received

## EXAMPLES

    set sock [ns_socklisten * 9000]
    ns_sockcallback $sock newconn r
    # Keep $sock from closing after connection ends
    detach $sock
    # When a connection arrives, newconn will be called as:
    # newconn $sock r
    proc newconn {sock when} {
      set fds [ns_sockaccept $sock]
      set rfd [lindex $fds 0]
      set wfd [lindex $fds 1]
      puts $wfd "Hello!"
      close $rfd
      close $wfd
    }

## See Also

ns_connchan, ns_socklisten

## Keywords

NaviServer, callback, socket