function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"$1"); } function setMaybeNick (input) { var match = /^\/nick\s+(.+)$/.exec(input); if (match) { nick = match[1]; } } var nick; $(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(); safe_message = replaceURLWithHTMLLinks(safe_message); 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(); setMaybeNick($('#input').val()); var sendObj = { message: $('#input').val(), }; if (typeof nick === 'string') { sendObj.nick = nick; }; sock.send(JSON.stringify(sendObj)); $('#input').val(''); return; } }); });