HTML and CSS Reference
In-Depth Information
4.
replace the existing script element with the code shown in Listing 13-16.
Listing 13-16. Adding the JavaScript
<script type="text/javascript">
var ws; // This is our socket
function connect() {
output("Connecting to host...");
try {
ws=new WebSocket("ws://localhost:8300/chat");
} catch (e) {
output(e);
}
ws.onopen=function () {
output("connected... ");
var p=document.getElementById("name");
ws.send("[Client SignOn:"+p.value+"]");
};
ws.onmessage=function (e) {
displayMsg(e.data);
};
ws.onclose=function () {
output("Connection closed");
};
};
function send() {
var input=document.getElementById("input");
try {
ws.send(input.value);
var o=document.getElementById("chat1");
o.innerHTML=o.innerHTML+"<p><b>Me:</b>"+input.value+"</p>";
input.value="";
} catch (e) {
output(e);
}
}
function output(msg) {
var o=document.getElementById("output");
o.innerHTML=o.innerHTML+"<p>"+msg+"</p>";
};
 
Search WWH ::




Custom Search