From ff67c4b7215358cf785c058201328ff6a0f5fc2b Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 5 Nov 2013 19:50:49 +0100 Subject: webchat: now in git --- webchat/public/client.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 webchat/public/client.js (limited to 'webchat/public/client.js') 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 = $('
').text(object.message).html() + var safe_from = $('
').text(object.from).html() + $('#chatbox').append(''+safe_from+''+safe_message+''); + + } 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; + } + }); + +}); -- cgit v1.2.3