chat-ws
library moduleA
Summary
Simple WebSocket chat. WebSocket functions.
- Authors
- BaseX Team 2005-23, BSD License
Imports
This module is imported by 0 modules. It imports 2 modules.
Variables
None
Functions
4.1 chat-ws:close
Arities: #0A
chat-ws:close
(
) as
empty-sequence
empty-sequence
Invoked by 0 functions from 0 modules
Annotations (1)
%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: #0A
chat-ws:connect
(
) as
empty-sequence
empty-sequence
Invoked by 0 functions from 0 modules
Annotations (1)
%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: #1A
chat-ws:message
(
$message
as
xs:string
) as
empty-sequence
- message
as
xs:string
message
empty-sequence
Invoked by 0 functions from 0 modules
Annotations (1)
%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 |
session | http://basex.org/modules/session |
ws | http://basex.org/modules/ws |
xs | http://www.w3.org/2001/XMLSchema |
Source Code
(:~
: Simple WebSocket chat. WebSocket functions.
: @author BaseX Team 2005-23, 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()
};