forked from JustMaier/angular-signalr-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalr-hub.js
82 lines (74 loc) · 2.52 KB
/
signalr-hub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
angular.module('SignalR', [])
.constant('$', $)
.factory('Hub', ['$', function ($) {
//This will allow same connection to be used for all Hubs
//It also keeps connection as singleton.
var globalConnections = [];
function initNewConnection(options) {
var connection = null;
if (options && options.rootPath) {
connection = $.hubConnection(options.rootPath, { useDefaultPath: false });
} else {
connection = $.hubConnection();
}
connection.logging = (options && options.logging ? true : false);
return connection;
}
function getConnection(options) {
var useSharedConnection = !(options && options.useSharedConnection === false);
if (useSharedConnection) {
return typeof globalConnections[options.rootPath] === 'undefined' ?
globalConnections[options.rootPath] = initNewConnection(options) :
globalConnections[options.rootPath];
}
else {
return initNewConnection(options);
}
}
return function (hubName, options) {
var Hub = this;
Hub.connection = getConnection(options);
Hub.proxy = Hub.connection.createHubProxy(hubName);
Hub.on = function (event, fn) {
Hub.proxy.on(event, fn);
};
Hub.invoke = function (method, args) {
return Hub.proxy.invoke.apply(Hub.proxy, arguments)
};
Hub.disconnect = function () {
Hub.connection.stop();
};
Hub.connect = function () {
return Hub.connection.start(options.transport ? { transport: options.transport } : null);
};
if (options && options.listeners) {
angular.forEach(options.listeners, function (fn, event) {
Hub.on(event, fn);
});
}
if (options && options.methods) {
angular.forEach(options.methods, function (method) {
Hub[method] = function () {
var args = $.makeArray(arguments);
args.unshift(method);
return Hub.invoke.apply(Hub, args);
};
});
}
if (options && options.queryParams) {
Hub.connection.qs = options.queryParams;
}
if (options && options.errorHandler) {
Hub.connection.error(options.errorHandler);
}
//Allow for the user of the hub to easily implement actions upon disconnected.
//e.g. : Laptop/PC sleep and reopen, one might want to automatically reconnect
//by using the disconnected event on the connection as the starting point.
if (options && options.hubDisconnected) {
Hub.connection.disconnected(options.hubDisconnected);
}
//Adding additional property of promise allows to access it in rest of the application.
Hub.promise = Hub.connect();
return Hub;
};
}]);