/
hubs.js
120 lines (98 loc) · 4.01 KB
/
hubs.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*!
* SignalR JavaScript Library v0.5.3
* http://signalr.net/
*
* Copyright David Fowler and Damian Edwards 2012
* Licensed under the MIT.
* https://github.com/SignalR/SignalR/blob/master/LICENSE.md
*
*/
/// <reference path="jquery-1.6.2.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window) {
/// <param name="$" type="jQuery" />
"use strict";
if (typeof ($.signalR) !== "function") {
throw "SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.";
}
var signalR = $.signalR;
function makeProxyCallback(hub, callback) {
return function () {
// Update the hub state
$.extend(hub, this.state);
// Call the client hub method
callback.apply(hub, $.makeArray(arguments));
};
}
function createHubProxies(instance, hubConnection) {
var key, hub, memberKey, memberValue, proxy;
for (key in instance) {
if (instance.hasOwnProperty(key)) {
hub = instance[key];
if (!(hub._ && hub._.hubName)) {
// Not a client hub
continue;
}
// Create and store the hub proxy
hub._.proxy = hubConnection.createProxy(hub._.hubName);
// Loop through all members on the hub and find client hub functions to subscribe to
for (memberKey in hub) {
if (hub.hasOwnProperty(memberKey)) {
memberValue = hub[memberKey];
if (memberKey === "_" || $.type(memberValue) !== "function"
|| $.inArray(memberKey, hub._.ignoreMembers) >= 0) {
// Not a client hub function
continue;
}
// Subscribe to the hub event for this method
hub._.proxy.on(memberKey, makeProxyCallback(hub, memberValue));
}
}
}
}
}
function copy(obj, exclude) {
var newObj = {};
$.each(obj, function (key, value) {
if (!$.isFunction(value) && $.inArray(key, exclude) === -1) {
// We don't use "this" because browsers suck!
newObj[key] = value;
}
});
return newObj;
}
function invoke(hub, methodName, args) {
// Extract user callback from args
var userCallback = args[args.length - 1], // last argument
callback = function (result) {
// Update hub state from proxy state
$.extend(hub, hub._.proxy.state);
if ($.isFunction(userCallback)) {
userCallback.call(hub, result);
}
},
connection = hub._.connection();
if ($.isFunction(userCallback)) {
// Replace user's callback with our own
args = $.merge(args.splice(0, args.length - 1), [callback]);
}
if (!hub._.proxy) {
if (connection.state === signalR.connectionState.disconnected) {
// Connection hasn't been started yet
throw "SignalR: Connection must be started before data can be sent. Call .start() before .send()";
}
if (connection.state === signalR.connectionState.connecting) {
// Connection hasn't been started yet
throw "SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.";
}
}
// Update proxy state from hub state
$.extend(hub._.proxy.state, copy(hub, ["_"]));
return hub._.proxy.invoke.apply(hub._.proxy, $.merge([methodName], args));
}
/*hubs*/
signalR.hub = $.hubConnection("{serviceUrl}")
.starting(function () {
createHubProxies(signalR, this);
});
}(window.jQuery, window));