diff options
| author | makefu <github@syntax-fehler.de> | 2013-11-05 23:40:11 +0100 | 
|---|---|---|
| committer | makefu <github@syntax-fehler.de> | 2013-11-05 23:40:11 +0100 | 
| commit | 940d07624c6b82d5e7ec790c2ef49b4694a48794 (patch) | |
| tree | 3f4bb46333429d8e9a4f566579f82cacbd3096c9 /webchat/public/client.js | |
| parent | fe1017207310d0cfd6448750205d08b2455f20a2 (diff) | |
| parent | 85c99011060b4b37a760fa24d0a0e23e83413bef (diff) | |
Merge branch 'master' of 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; +    } +  }); + +}); | 
