NaviServer Programmable Web Server

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

ns_sockcallback(n) 5.1.0 naviserver "NaviServer Built-in Commands"

Name

ns_sockcallback - Register a socket callback script

Table Of Contents

Synopsis

Description

Command for registering a socket callback script.

COMMANDS

ns_sockcallback sockId command when ?timeout?

Registers command as socket callback, which is fired based on the information in the when parameter. This parameter can consist of one or more characters of r, w, e, or x, specifying, when the callback should fire. The meaning of these characters is as follows:

  • r - the socket is readable

  • w - the socket is writable

  • e - the socket has an exceptional condition

  • x - the server is shutting down

The optional argument timeout can be specified in the form secs?:microsecs?, or secs.fraction, or as a number with a time unit.

When the callback is fired, the specified command is called with additional arguments sockId and condition. The value of the reported condition will be as follows:

  • r - the socket is readable

  • w - the socket is writable

  • e - the socket has an exceptional condition

  • x - the server is shutting down

  • t - timeout received

The ns_sock* command family is retained for backward compatibility with existing applications. New applications should use ns_connchan instead. In particular, connection-channel handling provides ns_connchan detach for cases where a channel should remain open independently of the current connection.

EXAMPLES

 #
 # Opening a listen socket
 #
 set sock [ns_socklisten * 9000]
 #
 # Register the proc "newconn", which is fired when the socket
 # becomes readable. When this is the case (e.g. a connection
 # arrives), "newconn" will be called as:  "newconn $sock r"
 #
 ns_sockcallback $sock newconn r
 
 #
 # Definition of the proc "newconn"
 #
 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_chan, ns_connchan, ns_sockaccept, ns_socklisten, ns_sockopen, ns_time

Keywords

NaviServer, callback, global built-in, socket