Changes

Jump to navigation Jump to search
589 bytes removed ,  19:39, 13 August 2018
no edit summary
* All functions and errors are assigned to the <code><nowiki>http://basex.org/modules/websocket</nowiki></code> namespace. The module must be imported in the query prolog:
<pre class="brush:xquery">
import module namespace websocket ws = "http://basex.org/modules/Websocket";
...
</pre>
=Functions=
==websocketws:send== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:send|$message as xs:anyAtomicType, $ids as xs:string* |empty-sequence()}}
|-
| '''Summary'''
|}
==websocketws:broadcast== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:broadcast|$message as xs:anyAtomicType|empty-sequence()}}
|-
| '''Summary'''
|}
==websocketws:emit== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:emit|$message as xs:anyAtomicType|empty-sequence()}}
|-
| '''Summary'''
|}
==websocketws:id== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:id||xs:string}}
|-
| '''Summary'''
|}
==websocketws:ids== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:ids||xs:string*}}
|-
| '''Summary'''
|}
==websocketws:path== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:path||xs:string}}
|-
| '''Summary'''
|}
==websocketws:path== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|websocketws:path|$id as xs:string|xs:string}}
|-
| '''Summary'''
=Example 1=
 
==Code==
 
<pre class="brush:xquery">
declare
module namespace websocketexample = 'http://basex.org/modules/web-page';
(: Import the WebSocket module :)
import module namespace websocket ws = "http://basex.org/modules/Websocket";
declare
%ws:connect("/")
function websocketexample:connect(
) { let $client-id := websocketws:id() let $client-path := websocketws:path() let $response := json:serialize( <json type="object"> <messageType>UserConnected</messageType> <clientId>{$client-id}</clientId> <clientPath>{$client-path}</clientPath> </json> ) return websocketws:broadcast($response) };
</pre>
 
==Explanation==
 
* First of all: include the websocket module
* The <code>$ws:connect("/")</code> annotation gets called if a client successfully creates a websocket to the path "/" (checkout [[WebSockets]] for further information).
* Get the <code>client-id</code> and the <code>client-path</code> with <code>websocketws:id()</code> and <code>websocketws:path()</code>
* Create a json-result
* Broadcast the result to all connected clients without the calling client
=Example 2=
 
==Code==
 
<pre class="brush:xquery">
module namespace websocketsexample = 'http://basex.org/modules/web-page';
(: Import the Websockets module :)
import module namespace websocket ws = "http://basex.org/modules/Websocket"; 
declare
%ws:message("/","{$message}")
function websocketsexamplelocal:message( $message as xs:string )as xs:string { let $client-ids := websocketws:ids() let $fist-client-id := fn:head($client-ids) let $send-to-id := websocketws:send($message,$first-client-id) let $path-first-client := websocketws:path($first-client-id) let $response := json:serialize( <json type="object"> <messageType>PathFirstClient</messageType> <path>{$path-first-client}</path> </json> ) return websocketws:emit($response) };
</pre>
 
==Explanatation==
 
* First of all, import the websocket module
* The annotation <code>$ws:message("/",{$message}")</code> gets called if a message arrives at the server (checkout [[WebSockets]] for further information).
* With <code>websocketws:ids()</code> you will get the ids of all connected clients.* The function <code>websocketws:send($message,$first-client-id)</code> sends the <code>$message</code> to the client with the id <code>$first-client-id</code>* <code>websocketws:path($first-client-id)</code> returns the path of the client with the id <code>$first-client-id</code>* To emit a message to all connected clients you call <code>websocketws:emit($response)</code>
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu