HTML and CSS Reference
In-Depth Information
Real-Time Communication) video conferencing. The data transmitted over WebSockets can
be text based or binary. The code in Listing 2-1 demonstrates the WebSocket API.
LISTING 2-1 Implementation of the WebSocket API
<script type="text/javascript">
window.onload = function (){
var wsConnection;
var chatBox = document.getElementById("chatWindow");
var disconnectButton = document.getElementById("Disconnect");
var connectButton = document.getElementById("Connect");
var sendButton = document.getElementById("Send");
var msgToSend = document.getElementById("msgSendText");
disconnectButton.onclick = function () {
wsConnection.close();
}
connectButton.onclick = function () {
//Or the use of wss for secure WebSockets. IE: wss://studygroup.70480.com
//Opens the WebSocket
wsConnection= new WebSocket('ws://studygroup.70480.com', ['soap', 'xmpp']);
}
sendButton.onclick = function () {
//check the state of the connection
if (wsConnection.readyState == WebSocket.OPEN) {
//send message to server.
wsConnection.send(msgToSend.value);
}
else
return;
//show message in chat window.
NewLine();
chatBox.value = chatBox.value + "You: " + msgToSend.value;
//clear message text box
msgToSend.value = '';
}
// event handler for when the WebSocket connection is established
wsConnection.onopen = function () {
chatBox.textContent = chatBox.textContent +
"System: Connection has been established";
}
//event handler for when the WebSocket encounters an error
wsConnection.onerror = function (err) {
//write an error to the screen
NewLine();
chatBox.value = chatBox.value + "System: Error Occurred. ";
}
wsConnection.onclose = function () {
//write the connection has been closed.
NewLine();
chatBox.value = chatBox.value + "System: Connection has been closed.";
}
Search WWH ::




Custom Search