chat-ws
library moduleA
Summary
Simple WebSocket chat. WebSocket functions.
- Tags
Author: BaseX Team 2005-21, BSD License
__source : chat-ws.xqm
Imports
This module is imported by 0 modules. It imports 2 modules.
Variables
None
Functions
4.1 chat-ws:close
Arities: chat-ws:close#0A
chat-ws:close
(
)
as
empty-sequence()
empty-sequence()
Invokes 3 functions from 2 modules
- chat-util:users#0
- {http://basex.org/modules/ws}delete#2
- {http://basex.org/modules/ws}id#0
Invoked by 0 functions from 0 modules
Annotations
%ws:close | ('/chat') |
Source ( 4 lines)
function chat-ws:close() as empty-sequence() {
ws:delete(ws:id(), $chat-util:id),
chat-util:users()
}
4.2 chat-ws:connect
Arities: chat-ws:connect#0A
chat-ws:connect
(
)
as
empty-sequence()
empty-sequence()
Invokes 4 functions from 3 modules
- chat-util:users#0
- {http://basex.org/modules/session}get#1
- {http://basex.org/modules/ws}id#0
- {http://basex.org/modules/ws}set#3
Invoked by 0 functions from 0 modules
Annotations
%ws:connect | ('/chat') |
Source ( 4 lines)
function chat-ws:connect() as empty-sequence() {
ws:set(ws:id(), $chat-util:id, session:get($chat-util:id)),
chat-util:users()
}
4.3 chat-ws:message
Arities: chat-ws:message#1A
chat-ws:message
(
$message
as
xs:string
)
as
empty-sequence()
- message
as
xs:string
message
empty-sequence()
Invokes 3 functions from 2 modules
- chat-util:message#2
- {http://www.w3.org/2005/xpath-functions}error#0
- {http://www.w3.org/2005/xpath-functions}parse-json#1
Invoked by 0 functions from 0 modules
Annotations
%ws:message | ('/chat','{$message}') |
Source ( 11 lines)
function chat-ws:message(
$message as xs:string
) as empty-sequence() {
let $json := parse-json($message)
let $type := $json?type
return if($type = 'message') then (
chat-util:message($json?text, $json?to)
) else if($type = 'ping') then(
(: do nothing :)
) else error()
}
Namespaces
The following namespaces are defined:
Prefix | Uri |
---|---|
chat-util | chat/util |
chat-ws | chat-ws |
request | http://exquery.org/ns/request |
ws | http://basex.org/modules/ws |
Source Code
(:~
: Simple WebSocket chat. WebSocket functions.
: @author BaseX Team 2005-21, BSD License
:)
module namespace chat-ws = 'chat-ws';
import module namespace chat-util = 'chat/util' at 'chat-util.xqm';
import module namespace request = "http://exquery.org/ns/request";
(:~
: Creates a WebSocket connection. Registers the user and notifies all clients.
:)
declare
%ws:connect('/chat')
function chat-ws:connect() as empty-sequence() {
ws:set(ws:id(), $chat-util:id, session:get($chat-util:id)),
chat-util:users()
};
(:~
: Processes a WebSocket message.
: @param $message message
:)
declare
%ws:message('/chat', '{$message}')
function chat-ws:message(
$message as xs:string
) as empty-sequence() {
let $json := parse-json($message)
let $type := $json?type
return if($type = 'message') then (
chat-util:message($json?text, $json?to)
) else if($type = 'ping') then(
(: do nothing :)
) else error()
};
(:~
: Closes a WebSocket connection. Unregisters the user and notifies all clients.
:)
declare
%ws:close('/chat')
function chat-ws:close() as empty-sequence() {
ws:delete(ws:id(), $chat-util:id),
chat-util:users()
};