Skip to content

Instantly share code, notes, and snippets.

@rosterloh
Created August 18, 2014 13:20
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 rosterloh/de06827bafe3c4a8454e to your computer and use it in GitHub Desktop.
Save rosterloh/de06827bafe3c4a8454e to your computer and use it in GitHub Desktop.
Angular Socket.io provider
/*
* Provider for Socket.io service
* first argument is the name of the service (not the name of the provider!) and second one is the constructor function for the provide
*/
//angular.module('module').provider('$socket', $socketProvider() {
var angular.module('socket.io', []);
module.provider('$socket', function $socketProvider() {
var ioUrl = '';
var ioConfig = {};
this.setConnectionUrl = function setConnectionUrl(url) {
if (typeof url == 'string') {
ioUrl = url;
} else {
throw new TypeError('url must be of type string');
}
};
function setOption(name, value, type) {
if (typeof value != type) {
throw new TypeError("'"+ name +"' must be of type '"+ type + "'");
}
ioConfig[name] = value;
};
this.setResource = function setResource(value) {
setOption('resource', value, 'string');
};
this.setConnectTimeout = function setConnectTimeout(value) {
setOption('connect timeout', value, 'number');
};
this.setTryMultipleTransports = function setTryMultipleTransports(value) {
setOption('try multiple transports', value, 'boolean');
};
this.setReconnect = function setReconnect(value) {
setOption('reconnect', value, 'boolean');
};
this.setReconnectionDelay = function setReconnectionDelay(value) {
setOption('reconnection delay', value, 'number');
};
this.setReconnectionLimit = function setReconnectionLimit(value) {
setOption('reconnection limit', value, 'number');
};
this.setMaxReconnectionAttempts = function setMaxReconnectionAttempts(value) {
setOption('max reconnection attempts', value, 'number');
};
this.setSyncDisconnectOnUnload = function setSyncDisconnectOnUnload(value) {
setOption('sync disconnect on unload', value, 'boolean');
};
this.setAutoConnect = function setAutoConnect(value) {
setOption('auto connect', value, 'boolean');
};
this.setFlashPolicyPort = function setFlashPolicyPort(value) {
setOption('flash policy port', value, 'number')
};
this.setForceNewConnection = function setForceNewConnection(value) {
setOption('force new connection', value, 'boolean');
};
this.$get = function $socketFactory($rootScope) {
var socket = io(ioUrl, ioConfig);
return {
on: function on(event, callback) {
socket.on(event, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
off: function off(event, callback) {
if (typeof callback == 'function') {
socket.removeListener(event, callback);
} else {
socket.removeAllListeners(event);
}
},
emit: function emit(event, data, callback) {
if (typeof callback == 'function') {
socket.emit(event, data, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
} else {
socket.emit(event, data);
}
}
};
};
}
/* USAGE
* app.config(function ($socketProvider) {
* $socketProvider.setConnectionUrl('http://localhost:8080');
* });
* app.controller('Ctrl', function Ctrl($scope, $socket) {
* $socket.on('echo', function (data) {
* $scope.serverResponse = data;
* });
* $scope.emitBasic = function emitBasic() {
* $socket.emit('echo', $scope.dataToSend);
* $scope.dataToSend = '';
* };
* $scope.emitACK = function emitACK() {
* $socket.emit('echo-ack', $scope.dataToSend, function (data) {
* $scope.serverResponseACK = data;
* });
* $scope.dataToSend = '';
* };
* });
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment