diff options
author | lassulus <lassulus@googlemail.com> | 2013-11-05 19:50:49 +0100 |
---|---|---|
committer | lassulus <lassulus@googlemail.com> | 2013-11-05 19:50:49 +0100 |
commit | ff67c4b7215358cf785c058201328ff6a0f5fc2b (patch) | |
tree | d04369c2884c9da79e339d5cb49dc9f508c7af14 /webchat/public/client.js | |
parent | 1d69388a39964c67a2e17483084565279e1fb550 (diff) |
webchat: now in git
Diffstat (limited to 'webchat/public/client.js')
-rw-r--r-- | webchat/public/client.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/webchat/public/client.js b/webchat/public/client.js new file mode 100644 index 00000000..119913ae --- /dev/null +++ b/webchat/public/client.js @@ -0,0 +1,44 @@ +$(function connect() { + sock = new SockJS('/echo'); + + sock.onopen = function() { + console.log('open'); + sock.send('open'); + }; + sock.onmessage = function(e) { + console.log('message', e.data); + try { + var object = JSON.parse(e.data); + console.log(object.message); + var safe_message = $('<div/>').text(object.message).html() + var safe_from = $('<div/>').text(object.from).html() + $('#chatbox').append('<tr><td class="from">'+safe_from+'</td><td>'+safe_message+'</td></tr>'); + + } catch (error) { + console.log(error); + } + }; + sock.onclose = function(event) { + console.log('close'); + switch (event.code) { + case 1006: //abnormal closure + return setTimeout(connect, 1000); + }; + }; + +}); +$(function() { + $("#input").keydown(function(e) { + if( e.keyCode === 13) { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + sock.send(JSON.stringify({ + message: $('#input').val(), + })); + $('#input').val(''); + return; + } + }); + +}); |