You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.2 KiB

7 years ago
  1. /*
  2. * @license
  3. * angular-socket-io v0.7.0
  4. * (c) 2014 Brian Ford http://briantford.com
  5. * License: MIT
  6. */
  7. angular.module('btford.socket-io', []).
  8. provider('socketFactory', function () {
  9. 'use strict';
  10. // when forwarding events, prefix the event name
  11. var defaultPrefix = 'socket:',
  12. ioSocket;
  13. // expose to provider
  14. this.$get = ['$rootScope', '$timeout', function ($rootScope, $timeout) {
  15. var asyncAngularify = function (socket, callback) {
  16. return callback ? function () {
  17. var args = arguments;
  18. $timeout(function () {
  19. callback.apply(socket, args);
  20. }, 0);
  21. } : angular.noop;
  22. };
  23. return function socketFactory (options) {
  24. options = options || {};
  25. var socket = options.ioSocket || io.connect();
  26. var prefix = options.prefix === undefined ? defaultPrefix : options.prefix ;
  27. var defaultScope = options.scope || $rootScope;
  28. var addListener = function (eventName, callback) {
  29. socket.on(eventName, callback.__ng = asyncAngularify(socket, callback));
  30. };
  31. var addOnceListener = function (eventName, callback) {
  32. socket.once(eventName, callback.__ng = asyncAngularify(socket, callback));
  33. };
  34. var wrappedSocket = {
  35. on: addListener,
  36. addListener: addListener,
  37. once: addOnceListener,
  38. emit: function (eventName, data, callback) {
  39. var lastIndex = arguments.length - 1;
  40. var callback = arguments[lastIndex];
  41. if(typeof callback == 'function') {
  42. callback = asyncAngularify(socket, callback);
  43. arguments[lastIndex] = callback;
  44. }
  45. return socket.emit.apply(socket, arguments);
  46. },
  47. removeListener: function (ev, fn) {
  48. if (fn && fn.__ng) {
  49. arguments[1] = fn.__ng;
  50. }
  51. return socket.removeListener.apply(socket, arguments);
  52. },
  53. removeAllListeners: function() {
  54. return socket.removeAllListeners.apply(socket, arguments);
  55. },
  56. disconnect: function (close) {
  57. return socket.disconnect(close);
  58. },
  59. connect: function() {
  60. return socket.connect();
  61. },
  62. // when socket.on('someEvent', fn (data) { ... }),
  63. // call scope.$broadcast('someEvent', data)
  64. forward: function (events, scope) {
  65. if (events instanceof Array === false) {
  66. events = [events];
  67. }
  68. if (!scope) {
  69. scope = defaultScope;
  70. }
  71. events.forEach(function (eventName) {
  72. var prefixedEvent = prefix + eventName;
  73. var forwardBroadcast = asyncAngularify(socket, function () {
  74. Array.prototype.unshift.call(arguments, prefixedEvent);
  75. scope.$broadcast.apply(scope, arguments);
  76. });
  77. scope.$on('$destroy', function () {
  78. socket.removeListener(eventName, forwardBroadcast);
  79. });
  80. socket.on(eventName, forwardBroadcast);
  81. });
  82. }
  83. };
  84. return wrappedSocket;
  85. };
  86. }];
  87. });