chat/util  library module

Summary

Simple WebSocket chat. Utility functions.
Authors
  • BaseX Team 2005-23, BSD License
Related documents
ViewDescriptionFormat
xqdocxqDoc xml file from the source modulexml
xqparsexqparse xml file from the source modulexml

Imports

This module is imported by 2 modules. It imports 2 modules.

imports
chat/util
imports
http://basex.org/modules/session
http://basex.org/modules/ws

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

Summary
Closes all WebSocket connections from the specified user.
Signatures
chat-util:close ( $name as xs:string ) as empty-sequence
Parameters
  • name as xs:string username
Return
  • 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

    Summary
    Sends a message to all clients, or to the clients of a specific user.
    Signatures
    chat-util:message ( $text as xs:string, $to as xs:string? ) as empty-sequence
    Parameters
    • text as xs:string text to be sent
    • to as xs:string? receiver of a private message (optional)
    Return
    • 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

      Summary
      Sends a users list (all, active) to all registered clients.
      Signatures
      chat-util:users ( ) as empty-sequence
      Return
      • 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:

        PrefixUri
        chat-utilchat/util
        sessionhttp://basex.org/modules/session
        userhttp://basex.org/modules/user
        wshttp://basex.org/modules/ws
        xshttp://www.w3.org/2001/XMLSchema

        6 RestXQ

        None

        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)
        };