46 lines
1.1 KiB
Text
46 lines
1.1 KiB
Text
(:~
|
|
: Simple WebSocket chat. WebSocket functions.
|
|
: @author BaseX Team, BSD License
|
|
:)
|
|
module namespace chat-ws = 'chat-ws';
|
|
|
|
import module namespace chat-util = 'chat/util' at 'lsp-util.xqm';
|
|
|
|
|
|
(:~
|
|
: Creates a WebSocket connection. Registers the user and notifies all clients.
|
|
:)
|
|
declare
|
|
%ws:connect('/lsp')
|
|
function chat-ws:connect() as empty-sequence() {
|
|
ws:set(ws:id()=>trace("CONNECT: "), $chat-util:id, session:get($chat-util:id)),
|
|
chat-util:users()
|
|
};
|
|
|
|
(:~
|
|
: Processes a WebSocket message.
|
|
: @param $message message
|
|
:)
|
|
declare
|
|
%ws:message('/lsp', '{$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('/lsp')
|
|
function chat-ws:close() as empty-sequence() {
|
|
ws:delete(ws:id(), $chat-util:id),
|
|
chat-util:users()
|
|
};
|