[mod] before esm

This commit is contained in:
Andy Bunce 2025-07-17 15:31:04 +01:00
parent 3c60660f16
commit 8df5f011bc
8 changed files with 147 additions and 22 deletions

View file

@ -2,24 +2,24 @@
: Simple WebSocket chat. Utility functions.
: @author BaseX Team, BSD License
:)
module namespace chat-util = 'chat/util';
module namespace lsp-util = 'chat/util';
import module namespace session = 'http://basex.org/modules/session';
import module namespace ws = 'http://basex.org/modules/ws';
(:~ User id (bound to sessions and WebSockets). :)
declare variable $chat-util:id := 'id';
declare variable $lsp-util:id := 'id';
(:~
: Sends a users list (all, active) to all registered clients.
:)
declare function chat-util:users() as empty-sequence() {
declare function lsp-util:users() as empty-sequence() {
ws:emit(map{
'type': 'users',
'users': array { sort(user:list()) },
'active': array { distinct-values(
for $id in ws:ids()
return ws:get($id, $chat-util:id)
return ws:get($id, $lsp-util:id)
)}
})
};
@ -29,15 +29,15 @@ declare function chat-util:users() as empty-sequence() {
: @param $text text to be sent
: @param $to receiver of a private message (optional)
:)
declare function chat-util:message(
declare function lsp-util:message(
$text as xs:string,
$to as xs:string?
) as empty-sequence() {
let $ws-ids := ws:ids()[not($to) or ws:get(., $chat-util:id) = $to]
let $ws-ids := ws:ids()[not($to) or ws:get(., $lsp-util:id) = $to]
return ws:send(map{
'type': 'message',
'text': serialize($text),
'from': ws:get(ws:id(), $chat-util:id),
'from': ws:get(ws:id(), $lsp-util:id),
'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
'private': boolean($to)
}, $ws-ids)
@ -47,10 +47,10 @@ declare function chat-util:message(
: Closes all WebSocket connections from the specified user.
: @param $name username
:)
declare function chat-util:close(
declare function lsp-util:close(
$name as xs:string
) as empty-sequence() {
for $id in ws:ids()
where ws:get($id, $chat-util:id) = $name
where ws:get($id, $lsp-util:id) = $name
return ws:close($id)
};

View file

@ -2,19 +2,24 @@
: Simple WebSocket chat. WebSocket functions.
: @author BaseX Team, BSD License
:)
module namespace chat-ws = 'chat-ws';
module namespace lsp-ws = 'lsp-ws';
import module namespace chat-util = 'chat/util' at 'lsp-util.xqm';
declare
%ws:error('/lsp', '{$error}')
function lsp-ws:error($error) {
trace($error,"ERR ")
};
(:~
: 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()
function lsp-ws:connect() as empty-sequence() {
ws:set(ws:id()=>trace("CONNECT: "), $chat-util:id, session:get($chat-util:id))
(: ,chat-util:users() :)
};
(:~
@ -23,10 +28,10 @@ function chat-ws:connect() as empty-sequence() {
:)
declare
%ws:message('/lsp', '{$message}')
function chat-ws:message(
function lsp-ws:message(
$message as xs:string
) as empty-sequence() {
let $json := parse-json($message)
let $json := parse-json($message=>trace("MSG "))
let $type := $json?type
return if($type = 'message') then (
chat-util:message($json?text, $json?to)
@ -40,7 +45,7 @@ function chat-ws:message(
:)
declare
%ws:close('/lsp')
function chat-ws:close() as empty-sequence() {
function lsp-ws:close() as empty-sequence() {
ws:delete(ws:id(), $chat-util:id),
chat-util:users()
};