Changes

Jump to navigation Jump to search
610 bytes removed ,  16:31, 31 August 2018
no edit summary
This page presents one of the [[Web Application]] services. It describes how to use the WebSockets API of BaseX. WebSocket is a communication protocol for providing full-duplex communication.
The WebSocket protocol was standardized in [https://tools.ietf.org/html/rfc6455 RFC 6455] by the IETF. After an initial HTTP-request it communicates , all communication takes place over a single TCP connection. The WebSocket protocol was standardized in RFC 6455 by the IETF. It creates the possibility In contrast to transponse real-time data from the HTTP protocol, a connection will be kept alive, and a server can send unsolicited data to the client and the client to the server.
In contrast to the HTTP protocol it enables the server to send unsolicited data to the client. For establishing a WebSocket connection there is , a WebSocket handshake request, send is sent by the client. The web server returns a WebSocket handshake response. This will be handled by jetty. After a If the handshake is successful handshake, the annotation <code>ws:connect</code> persistent connection will be called. The persistent connection is now open until an the client or the server closes it, an error occures occurs or a timeout happens. How long this The timeout goes can be set specified in the <code>web.xml</code> configuration file. It is possible to transmit all kind of data, binary or text.  For further information about the WebSocket protocol check out the RFC6455 spec.
=Introduction=
To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory:
 
* Functions will be invalidated and parsed again if the timestamp of their module changes.
<pre class="brush:xquery">
module namespace page = 'http://basex.org/modules/web-page';
 
import module namespace ws = "http://basex.org/modules/Websocket";
 
declare
%ws:connect("/")
function wsdbapage:connect(
) {
(: Do something after a client connects to the path "/" :)
};
</pre>
<pre class="brush:xquery">
module namespace page = 'http://basex.org/modules/web-page';
declare
%ws:message("/","{$message}")
function chatpage:message(
$message as xs:string
) {
(: Do something if a message arrives at the server :)
};
</pre>
 
<pre class="brush:xquery">
module namespace page = 'http://basex.org/modules/web-page';
import module namespace ws = "http://basex.org/modules/Websocket";
declare
%ws:close("/")
function chat:close() {
(: Build Return a close message with the id of the client who closes the connection :) let $client-id := ws:id() let $msg := json:serialize( <json type="object"> <type>WebsocketClosed</type> <idThatClosed>{$client-id}</idThatClosed> </json> ) (: Broadcast the message to all connected users except the client who closes the connection :) return ws:broadcast($msg) };
</pre>
* Enable the WebSocket servlet in the web.xml. You can set here the maxIdleTime, maxTextMessageSize and maxBinaryMessageSize too.
* If you get a message that exceeds the maxTextMessageSize/maxBinaryMessageSize or, if not set, the default messageSize of Jetty of 65 536 bytes (64 kB) then the connection will close. In this case, the ws:error annotation will be called.
 
<pre class="brush:xml">
<servlet>
==ws:connect(path)==
Called when directly after a client successful WebSocket connection successfully connected to the serverhandshake. The <code>path</code> specifies the path the to which a client is connected to.
==ws:message(path,message)==
Called when a <code>message</code> arrives at the server. The <code>path</code> specifies the <code>path</code> the to which a client is connected to. The <code>message</code> is the <code>message</code> sent by the client. Could be a text-message or a binary-message.
==ws:close(path)==
Called when the WebSocket closes. The <code>path</code> specifies the <code>path</code> the to which a client is connected to.
The WebSocket is already closed when this annotation is called so there can be no return.
==ws:error(path,message)==Called when an error occurred. Usually, this happens because of bad/malformed incoming packets. The <code>path</code> specifies the <code>path</code> the client is connected to. The <code>message</code> is the error-message.The WebSocket gets closed after an error occurred. ==ws:path(name,variable[,default]==
Called when an error has occurred. Usually, this happens because of bad/malformed incoming packets. The value of <code>path</code> specifies the first parameter will be assigned path to which a client is connected to. The <code>message</code> is the variable specified as the second parametererror message. The third parameter can be a default valueWebSocket connection gets closed after the error handling.
==ws:header-param(name,variable[,default]==
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu