Changes

Jump to navigation Jump to search
1,777 bytes added ,  18:35, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
* The module will be available if the {{Code|basex-api}} library is found in the classpath. This is the case if you use one of the complete distributions of BaseX (zip, exe, war).
* All functions and errors are assigned to the <code><nowiki>http://basex.org/modules/ws</nowiki></code> namespace. The module must be imported in the query prolog: <pre class="brush:xquery">import module namespace ws = "http://basex.org/modules/ws";...</pre> * In this document, the namespace which is statically bound to the {{Code|ws}} prefix.* As sessions are side-effecting operations, all functions are flagged as ''non-deterministicnondeterministic''. As a result, some query optimizations will be suppressed.
=General Functions=
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:id||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the ID of the current WebSocket.
|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:ids||() as xs:string*}}</pre>|-valign="top"
| '''Summary'''
|Returns the ids of all currently registered WebSocketWebSockets.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:path|( $id as xs:string|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the path of the WebSocket with the specified {{Code|$id}}.
|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:close|( $id as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Closes the connection of the WebSocket with the specified {{Code|$id}}.
|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:send|( $message as item(), $ids as xs:string*|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Sends a <code>$message</code> to the clients with the specified <code>$ids</code>. Ids that cannot be assigned to clients will be ignored. The message will be handled as follows:
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:broadcast|( $message as xs:anyAtomicType|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Broadcasts a <code>$message</code> to all connected clients except to the caller. Invocations of this convenience function are equivalent to <code>ws:send($message, ws:ids()[. != ws:id()])</code>. See [[#ws:send{{Function||ws:send]] }} for more details on the message handling.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:emit|( $message as xs:anyAtomicType|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Emits a <code>$message</code> to all connected clients. Invocations of this function are equivalent to <code>ws:send($message, ws:ids())</code>. See [[#{{Function||ws:send}} for more details on the message handling.|} ==ws:eval== {| width='100%'|- valign="top"| width='120' |'''Signature'''|<pre>ws:send]] eval( $query as xs:anyAtomicType, $bindings as map(*)? := (), $options as map(*)? := map { }) as xs:string</pre>|- valign="top"| '''Summary'''|Schedules the evaluation of the supplied {{Code|$query}} and returns the result to the calling WebSocket client. The query can be a URI or a string, and variables and context items can be declared via {{Code|$bindings}} (see {{Function|XQuery|xquery:eval}} for more details on ). The following {{Code|$options}} can be supplied:* {{Code|base-uri}}: sets the [https://www.w3.org/TR/xquery-31/#dt-static-base-uri base-uri property] for the query. This URI will be used when resolving relative URIs, such as with {{Code|fn:doc}}.* {{Code|id}}: sets a custom job id. The id must not start with the standard <code>job</code> prefix, and it can only be assigned if no job with the same name exists.Query scheduling is recommendable if the immediate query execution might be too time consuming and lead to a timeout.|- valign="top"| '''Errors'''|{{Error|overflow|#Errors}} Query execution is rejected, because too many jobs are queued or being executed. <br/>{{Error|id|#Errors}} The specified id is invalid or has already been assigned.|- valign="top"| '''Examples'''|* Schedule a second query that will notify the client 10 seconds later that a message handlingwas processed:<pre lang='xquery'>declare %ws:message('/tasks', '{$message}')function local:message($message) { ws:eval('prof:sleep(10000), "Your message has been processed."')}; </pre>
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|ws:get|$id as xs:string, $name as xs:string|item()*}}<br/pre>{{Func|ws:get|( $id as xs:string, $name as xs:string, $default as item()*| := ()) as item()*}}</pre>|-valign="top"
| '''Summary'''
|Returns the value of an attribute with the specified {{Code|$name}} from for the WebSocket with the specified {{Code|$id}}. If the attribute is unknown, an empty sequence or the optionally specified {{Code|$default}} value will be returned instead.|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:set|( $id as xs:string, $name as xs:string, $value as item()*|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Returns Assigns the specified {{Code|$value}} of to the attribute with the specified {{Code|$name}} from for the WebSocket with the specified {{Mono|$id}}.|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.<br>{{Error|set|#Errors}} The supplied value cannot be materialized.
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>ws:delete|( $id as xs:string, $name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
| '''Summary'''
|Deletes an attribute with the specified {{Code|$name}} from the WebSocket with the specified {{Mono|$id}}.
|-valign="top"
| '''Errors'''
|{{Error|not-found|#Errors}} No WebSocket with the specified id exists.
==Example 1==
<pre classlang="brush:'xquery"'>
import module namespace ws = "http://basex.org/modules/ws";
==Example 2==
<pre classlang="brush:'xquery"'>
import module namespace ws = "http://basex.org/modules/ws";
! width="110"|Code
|Description
|-|{{Code|set}}|The supplied value cannot be materialized.|-valign="top"
|{{Code|not-found}}
|No WebSocket with the specified id exists.
=Changelog=
 
;Version 9.2
 
* Added: {{Function||ws:eval}}
This module was introduced with Version 9.1.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu