--- miscJava/Test3/WebContent/WebsockTest.html 2014/02/11 10:02:13 2112 +++ miscJava/Test3/src/main/webapp/WebsockTest.html 2015/03/20 08:12:57 2446 @@ -22,9 +22,12 @@ function sendMessage() { try { var txt = $("#txt").val(); - append(txt); + txt = $.trim(txt); + //append(txt); + if (txt == "") + return; - socket.send(txt); + socket.send( "CHAT#" + txt); $("#txt").val(""); setActTime(); @@ -54,6 +57,10 @@ setActTime(); } + socket.onerror = function(evt) { + append("*** Error om client websocket ***"); + } + socket.onclose = function(){ var now = new Date().getTime(); var elapsed = now - actTime; @@ -69,12 +76,33 @@ sendMessage(); }); + $("#btnNick").click( function() { + var nick = $("#nick").val(); + nick = $.trim(nick); + + if (nick == "") { + alert("Nick can not be empty"); + return; + } + + try { + socket.send( "NICK#" + nick); + } catch(exception) { + append(exception); + } + + }); + $(document).keypress(function(e) { if(e.which == 13) { sendMessage(); } }); + $(window).unload(function(){ + socket.close(); + }); + }); @@ -88,7 +116,10 @@
- +

+ + +