Changes

Jump to navigation Jump to search
280 bytes removed ,  17:43, 28 August 2018
no edit summary
==Examples==
 
<pre class="brush:xquery">
module namespace page = 'http://basex.org/modules/web-page';
declare
%ws:connect("/") function wsdba:connect( ) { (: Do something after a client connects to the path "/" :) };
</pre>
declare
%ws:message("/","{$message}")
function chat: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 the websocket module :)import module namespace websocket ws = "http://basex.org/modules/Websocket"; 
declare
%ws:close("/")
function chat:close( ){ (: Build a close message with the id of the client who closes the connection :) let $client-id := websocketws: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 websocketws:broadcast($msg)
};
</pre>
=Usage=
 
* 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 websocketws:error annotation will be called.
<pre class="brush:xml">
<servlet>
<servlet-name>wsservlet</servlet-name> <servlet-class>org.basex.http.ws.WsServlet</servlet-class> <init-param> <param-name>maxIdleTime</param-name> <param-value>100000</param-value> </init-param> <init-param> <param-name>maxTextMessageSize</param-name> <param-value>3000</param-value> </init-param>
</servlet>
<servlet-mapping>
<servlet-name>wsservlet</servlet-name> <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</pre>
=Annotations=
 ==websocketws:connect(path)== 
Called when a client WebSocket connection successfully connected to the server. The <code>path</code> specifies the path the client connected to.
==websocketws:message(path,message)== 
Called when a <code>message</code> arrives at the server. The <code>path</code> specifies the <code>path</code> the 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.
==websocketws:close(path)== 
Called when the WebSocket closes. The <code>path</code> specifies the <code>path</code> the client is connected to.
The WebSocket is already closed when this annotation is called so there can be no return.
==websocketws: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.
==websocketws:path(name,variable[,default]== 
The value of the first parameter will be assigned to the variable specified as the second parameter. The third parameter can be a default value.
==websocketws:header-param(name,variable[,default]== 
For accessing specific parameters like the Http-Version or the Sec-WebSocket-Version.
=Parameters=
 
* Http-Version -> f.e.: ```%ws:param("Http-Version", "{$version}")```
* Origin
=Tipps=
 
* For interacting with other clients or manage specific clients you should check out the [[WebSocket Module]] as well.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu