var start = function() { var inc = document.getElementById('incoming'); var wsImpl = window.WebSocket || window.MozWebSocket; var form = document.getElementById('sendForm'); var input = document.getElementById('sendText'); inc.innerHTML += "Connecting to server..
"; window.ws = new wsImpl('ws://' + location.hostname + ':509/'); //when a message is recived ws.onmessage = function (evt) { inc.innerHTML += 'Recived< ' + evt.data + '
'; } //when a connection is established ws.onopen = function() { inc.innerHTML += '..connection opened
'; } //when the connection is closed ws.onclose = function() { inc.innerHTML += '..connection closed
'; } form.addEventListner('submit', function(e) { e.preventDefault(); var val = input.value; ws.send(val); input.value = ""; }); } function send() { var val = document.getElementById('sendText').value; ws.send(val); inc.innerHTML += 'Sent-> ' + val + '
'; } //call the above created function on page start window.onload = start;