Java Reference
In-Depth Information
The message parameter of the onMessage() method represents the value of the
message that will be sent from the client. If our onMessage() method receives the
get_defaults string from the client, it generates a JSON string with default values
that will be used to populate the form.
Typically, messages sent from clients are JSON-formatted strings.
In our simple example, we are just using an arbitrary string.
The JSON string will then need to be parsed by JavaScript on the client side. In order
to implement this last piece of the puzzle, we need to add some JavaScript to our
JSF markup.
Implementing WebSocket functionality
on the client
Now, we need add JavaScript code to our markup to interact with our WebSocket
server endpoint. Take a look at the following code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head jsf:id="head">
<title>WebSocket and Java EE</title>
<script language="javascript" type="text/javascript">
var wsUri = getRootUri() +
"/WebSocketJavaEE/defaultdataendpoint";
function getRootUri() {
return "ws://" + (document.location.hostname == "" ?
"localhost" : document.location.hostname) + ":" +
(document.location.port == "" ? "8080" :
document.location.port);
}
function init() {
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
onOpen(evt)
};
 
Search WWH ::




Custom Search