Skip to content

Instantly share code, notes, and snippets.

@LukasGasior1
Last active August 29, 2015 14:22
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 LukasGasior1/5ce253c2b5883c4e3f5e to your computer and use it in GitHub Desktop.
Save LukasGasior1/5ce253c2b5883c4e3f5e to your computer and use it in GitHub Desktop.
module.service('commandService', function($http) {
return {
createGame: function(success, error) {
$http.post('/game')
.success(success)
.error(error);
},
startGame: function(gameId, playersCount, error) {
$http.post('/game/' + gameId + '/start?playersCount=' + playersCount)
.error(error);
},
roll: function(gameId, player, error) {
$http.post('/game/' + gameId + '/roll/' + player)
.error(error);
}
};
});
module.service('eventService', function($rootScope) {
var ws = null;
return {
connect: function(gameId) {
ws = new WebSocket('ws://' + window.location.hostname + ':' + window.location.port + '/' + gameId + '/events');
ws.onmessage = function(event) {
var message = JSON.parse(event.data);
$rootScope.$broadcast('events.' + message.eventType, message.data);
};
},
disconnect: function() {
if (ws != null) {
ws.close();
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment