Skip to content

Instantly share code, notes, and snippets.

@odadoda
Created November 8, 2013 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odadoda/7370977 to your computer and use it in GitHub Desktop.
Save odadoda/7370977 to your computer and use it in GitHub Desktop.
Clientside
$(function(){
// get websocket class, firefox has a different way to get it
var WS = window['MozWebSocket'] ? window['MozWebSocket'] : WebSocket;
// open pewpew with websocket
var socket = new WS('@routes.Application.wsInterface().webSocketURL(request)');
var writeMessages = function(event){
$('#socket-messages').prepend('<p>'+event.data+'</p>');
}
socket.onmessage = writeMessages;
// if enter (charcode 13) is pushed, send message, then clear input field
$('#socket-input').keyup(function(event){
var charCode = (event.which) ? event.which : event.keyCode ;
if(charCode === 13){
socket.send($(this).val());
$(this).val('');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment