Websh can send output to any Tcl channel and to global variables (web::put). Optionally, data is scanned for Tcl code before it is output to a channel (web::putx). Websh manages response objects that are related to Tcl channels and are identified using the name of the corresponding Tcl channel. Configuration is achieved with web::response.
web::response
web::response ?option?
web::response ?subcommand? argsSubcommands are -select, -set, -lappend, -names, -count, -unset, -reset, -resetall, and -flush. Options are -sendheader, -httpresponse, and -bytessent.
Selects the default response object and sets and accesses properties of the response object, and returns the name of the response object.
channelName
channelName
as new response object. If the
channelName is
prepended by a #, it refers to a global
variable named
channelName.
key
?value?web::response -set Content-Type text/plain.
keykey?value?web::response -set Status "400 Bad Request".
Example 10. web::response
% web::response stdout % web::response -select stderr stdout % web::response stderr % web::response -sendheader 1 % web::response -names Content-Type Generator % web::response Content-Type text/html % web::response -bytessent 0 % web::response -set Set-Cookie "my cookie that contains data" % web::put "Hello, world\n" Content-Type: text/html Set-Cookie: my cookie that contains data Generator: websh3.6.0 Hello, world %
web::put ?
?#?
channel
? text
Sends output to a Tcl channel. No newline is added to
output. If
?channel?
is ommitted, output is sent to the current default
channel. The default channel can be changed with
web::response -select
?#?channel.
The optional hash ("#") denotes that output should be
sent to a global variable named
channel instead of
a Tcl channel.
web::putx ?
?#?
channel
? text
Writes text to the specified channel. Code in
curly brackets is eval'd, unless the brackets are escaped by
"\". These markup characters '{...}' can be changed
to '<? ... ?>' with
'web::config putxmarkup tag'. The optional hash
("#") denotes that output should be sent to a global
variable named channel
instead of a Tcl channel.
web::putxfile ?
?#?
channel
? file ?msg?Like web::putx, but takes input from a file.
Returns 0 on success, 1 otherwise. If an error occurs, an
error message is written to msg. If only two
arguments are passed, then channel takes
precedence. The optional hash ("#") denotes that output
should be sent to a global variable named
channel instead
of a Tcl channel.
web::sseStart
Starts a Server-Sent Events (SSE) response. Sets the response
headers Content-Type: text/event-stream,
Cache-Control: no-cache,
Connection: keep-alive and
X-Accel-Buffering: no, and disables
buffering on the response channel. Use
web::sseSend to emit events afterwards.
web::sseSend data ?event? ?id? ?retry?
Sends one SSE event and flushes the response via
web::response -flush. Multiline
data is split on newlines and
emitted as one data: line per segment; the
event is terminated with a blank line. The optional arguments
emit event:, id: and
retry: fields.
For keepalive in long-running connections send an SSE comment
(a line starting with ":") followed by
web::response -flush; comments are ignored
by browsers but prevent proxy timeouts. See
examples/sse/ for a complete
server/client example.