Skip to content

Instantly share code, notes, and snippets.

@Landish
Forked from simon-jouet/angularjs Cometd
Last active August 29, 2015 14:17
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 Landish/c0fbb107ef68ad245fed to your computer and use it in GitHub Desktop.
Save Landish/c0fbb107ef68ad245fed to your computer and use it in GitHub Desktop.
factory('cometd', function($rootScope) {
var cometd = $.cometd;
// Configure cometd
cometd.configure({
url: location.protocol + '//' + location.host + config.contextPath + '/cometd',
logLevel: 'info'
});
// Add a listener for the handshake *TODO* what should be done if message fails ?
cometd.addListener('/meta/handshake', function(message) { console.log(message) });
// Handshake with the server
cometd.handshake();
return {
addListener: function(channel, scope, callback) {
cometd.addListener(channel, scope, function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(cometd, args);
})
});
},
removeListener: function(subscription) {
cometd.removeListener(subscription);
},
subscribe: function(channel, scope, callback, subscribeProps) {
cometd.subscribe(channel, scope, function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(cometd, args);
})
}, subscribeProps);
},
unsubscribe: function(subscription, unsubscribeProps) {
cometd.unsubscribe(subscription, unsubscribeProps);
},
publish: function(channel, content, publishProps, publishCallback) {
cometd.publish(channel, content, publishProps, function() {
var args = arguments;
$rootScope.$apply(function() {
publishCallback.apply(cometd, args);
})
});
},
disconnect: function(sync, disconnectProps) {
cometd.disconnect(sync, disconnectProps);
},
batch: function(scope, callback) {
cometd.batch(scope, function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(cometd, args);
})
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment