chat/util library module
Summary
Simple WebSocket chat. Utility functions.
- Authors
- BaseX Team 2005-23, BSD License
Imports
This module is imported by 2 modules. It imports 2 modules.
Variables
3.1 $chat-util:id
- Summary
- User id (bound to sessions and WebSockets).
- Type
Source ( 1 lines)
variable $chat-util:id := 'id'
Functions
4.1 chat-util:close
Arities: #1
chat-util:close
(
$name
as
xs:string
) as
empty-sequence
- name
as
xs:string
username
empty-sequence
Invoked by 0 functions from 0 modules
Source ( 7 lines)
function chat-util:close(
$name as xs:string
) as empty-sequence() {
for $id in ws:ids()
where ws:get($id, $chat-util:id) = $name
return ws:close($id)
}
4.2 chat-util:message
Arities: #2
chat-util:message
(
$text
as
xs:string
, $to
as
xs:string?
) as
empty-sequence
- text
as
xs:string
text to be sent - to
as
xs:string?
receiver of a private message (optional)
empty-sequence
Invoked by 0 functions from 0 modules
Source ( 13 lines)
function chat-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]
return ws:send(map {
'type': 'message',
'text': serialize($text),
'from': ws:get(ws:id(), $chat-util:id),
'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
'private': boolean($to)
}, $ws-ids)
}
4.3 chat-util:users
Arities: #0
chat-util:users
(
) as
empty-sequence
empty-sequence
Invoked by 0 functions from 0 modules
Source ( 10 lines)
function chat-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)
)}
})
}
Namespaces
The following namespaces are defined:
Prefix | Uri |
---|---|
chat-util | chat/util |
session | http://basex.org/modules/session |
user | http://basex.org/modules/user |
ws | http://basex.org/modules/ws |
xs | http://www.w3.org/2001/XMLSchema |
Source Code
(:~
: Simple WebSocket chat. Utility functions.
: @author BaseX Team 2005-23, BSD License
:)
module namespace chat-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';
(:~
: Sends a users list (all, active) to all registered clients.
:)
declare function chat-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)
)}
})
};
(:~
: Sends a message to all clients, or to the clients of a specific user.
: @param $text text to be sent
: @param $to receiver of a private message (optional)
:)
declare function chat-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]
return ws:send(map {
'type': 'message',
'text': serialize($text),
'from': ws:get(ws:id(), $chat-util:id),
'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
'private': boolean($to)
}, $ws-ids)
};
(:~
: Closes all WebSocket connections from the specified user.
: @param $name username
:)
declare function chat-util:close(
$name as xs:string
) as empty-sequence() {
for $id in ws:ids()
where ws:get($id, $chat-util:id) = $name
return ws:close($id)
};