diff options
author | tv <tv@nomic.retiolum> | 2013-11-05 20:16:20 +0100 |
---|---|---|
committer | tv <tv@nomic.retiolum> | 2013-11-05 20:16:20 +0100 |
commit | af52a313b7c7601bb2ca6a29253fbba26e632e96 (patch) | |
tree | beb0c96382952b6bb55ac49a7deed3c279d1fe93 /webchat/public/client.js | |
parent | 01ddf924ae017b71470d1e83124f69a53566e62c (diff) | |
parent | 241f13cd2880c97b9d3503725a0720a017779ed6 (diff) |
Merge branch 'master' of https://github.com/krebscode/painload
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; + } + }); + +}); |