Java Reference
In-Depth Information
Client endpoint
Our client WebSocket endpoint is a .jsp web page ( websocketChatCli-
ent.jsp ) which is based on JavaScript code. As you can see, the client side has
the same lifecycle methods and through the power of JSON, we can easily access
and display messages sent by the server.
The following code is an example of a web client WebSocket endpoint:
//complete URI of the chat server endpoint
var clientUri =
"ws://"+document.location.host+"/
chapter02NewSpecifications/chatserver";
var wsocket;
//connection request when loading the web page
window.addEventListener("load", connect, false);
//Connection method
function connect() {
wsocket = new WebSocket(clientUri);
//binding of the websocket lifecycle methods
wsocket.onmessage = onMessage;
wsocket.onerror = onError;
}
function joinChatRoom() {//method to join the
chat room
wsocket.send("ID-" + txtMessage.value);
}
function sendMessage() {//method to send a
message to the chat room
wsocket.send("M-" + txtMessage.value);
}
function onMessage(event) {//method to perform
incoming messages
Search WWH ::




Custom Search