(:~ : Simple WebSocket chat. RESTXQ functions. : @author BaseX Team, BSD License :) module namespace chat = 'chat'; import module namespace chat-util = 'chat/util' at 'lsp-util.xqm'; (:~ : Login or main page. : @return HTML page :) declare %rest:path('/lsp') %output:method('html') function chat:chat() as element() { if(session:get($chat-util:id)) then ( chat:main() ) else ( chat:login() ) }; (:~ : Checks the user input, registers the user and reloads the chat. : @param $name username : @param $pass password : @return redirection :) declare %rest:POST %rest:path('/lsp/login-check') %rest:form-param('name', '{$name}') %rest:form-param('pass', '{$pass}') function chat:login-check( $name as xs:string, $pass as xs:string ) as element(rest:response) { try { user:check($name, $pass), session:set($chat-util:id, $name) } catch user:* { (: login fails: no session info is set :) }, web:redirect('/chat') }; (:~ : Logs out the current user, notifies all WebSocket clients, and redirects to the login page. : @return redirection :) declare %rest:path('/lsp/logout') function chat:logout() as element(rest:response) { session:get($chat-util:id) ! chat-util:close(.), session:delete($chat-util:id), web:redirect('/chat') }; (:~ : Returns the HTML login page. : @return HTML page :) declare %private function chat:login() as element(html) { chat:wrap((
,
USERS (online)
|
CHAT MESSAGES
|