Changes

Jump to navigation Jump to search
805 bytes removed ,  11:34, 27 September 2018
no edit summary
|-
| width='120' | '''Signatures'''
|{{Func|ws:send|$message as xs:anyAtomicType, $ids as xs:string* |empty-sequence()}}
|-
| '''Summary'''
|Sends a <code>$message</code> , which may be of type xs:string, xs:base64Binary, or xs:hexBinary , to the user(s) users with the ID(s) specified <code>$ids</code>.
|}
|-
| '''Summary'''
|Broadcasts a <code>$message</code> , which may be of type xs:string, xs:base64Binary, or xs:hexBinary , to all connected clients except to the caller.
|}
|-
| '''Summary'''
|Emits a <code>$message</code> , which may be of type xs:string, xs:base64Binary, or xs:hexBinary , to all connected clients.
|}
|-
| '''Summary'''
|Returns the ids of all currently registered WebSocket clientclients.
|}
|-
| width='120' | '''Signatures'''
|{{Func|ws:path||xs:string}}<br>{{Func|ws:path|$id as xs:string|xs:string}}
|-
| '''Summary'''
|Returns the path of the current WebSocket client, or with the specified {{Code|$id}}.|} ==ws:close== {| width='100%'|-| width='120' | '''Signatures'''|{{Func|ws:close|$id as xs:string|empty-sequence()}}|-| '''Summary'''|Closes the connection of the WebSocket client with the specified {{Code|$id}}.
|}
<pre class="brush:xquery">
(: Import the WebSocket module :)
import module namespace ws = "http://basex.org/modules/Websocket";
declare
%ws:connect("'/"')function local:connect() as empty-sequence() xs:string { let $client-id := ws:id() let $client-path := ws:path() let $response message := json:serialize(map { <json 'type="object">': 'Connect', <messageType>UserConnected</messageType> <clientId>{ 'id': $client-id}</clientId> <clientPath>{$client-path }</clientPath> </json> ) return ws:broadcast($responsemessage)
};
</pre>
'''Explanation:'''
* First of all: include the websocket module* The function has a <code>$%ws:connect("/")</code> annotation . It gets called if a client successfully creates a websocket WebSocket connection to the path "<code>/</" code> (checkout check out [[WebSockets]] for further information).* Get A JSON response is generated, which contains the <code>new client-id</code> and the a <code>client-path</code> with <code>ws:id()</code> and <code>ws:path()Connect</code>string.* Create a json-result * Broadcast the result This response will be sent to all other connected clients without the calling client.
==Example 2==
<pre class="brush:xquery">
(: Import the Websockets module :)
import module namespace ws = "http://basex.org/modules/Websocket";
declare
%ws:message("'/"', "'{$message}"')
function local:message(
$message as xs:string) as empty-sequence() xs:string { let $client-ids := ws:ids() let $fist-client-id := fn:head($client-ids) let $send-to-id := ws:send($message, $first-client-id) let $path-first-client := ws:path($first-client-id) let $response := json:serialize( <json type="object"> <messageType>PathFirstClient</messageType> <path>map { 'message': $path-first-client message }</path> </json> ) return ws:emit($responsemessage)
};
</pre>
'''Explanation:'''
* First of all, import the websocket module* The annotation function has a <code>$%ws:message("/",{$message}")</code> annotation. It gets called if a client sends a new message arrives at the server (checkout [[WebSockets]] for further information).* With <code>ws:ids()</code> you will get the ids of all connected clients.* The function <code>ws:send($messageA JSON response is generated,$first-client-id)</code> sends which contains the <code>$message</code> to the client with the id <code>$first-client-id</code>string.* <code>ws:path($first-client-id)</code> returns the path of the client with the id <code>$first-client-id</code>* To emit a message This response will be sent to all connected clients you call <code>ws:emit($responseincluding the calling client)</code>.
Bureaucrats, editor, reviewer, Administrators
12,226

edits

Navigation menu