diff options
Diffstat (limited to 'point/libs/webcastor')
-rw-r--r-- | point/libs/webcastor/webcastor.js | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/point/libs/webcastor/webcastor.js b/point/libs/webcastor/webcastor.js new file mode 100644 index 0000000..734430c --- /dev/null +++ b/point/libs/webcastor/webcastor.js | |||
@@ -0,0 +1,156 @@ | |||
1 | define(function () { | ||
2 | |||
3 | var webcastor = { | ||
4 | |||
5 | STATUS: { | ||
6 | CONNECTED: "connected", | ||
7 | NOT_CONNECTED: "notConnected", | ||
8 | ERROR: "error", | ||
9 | AUTHENTICATED: "authenticated", | ||
10 | AUTHENTICATION_ERROR: "authenticationError", | ||
11 | }, | ||
12 | |||
13 | EVENT: { | ||
14 | CONNECT: "connect", | ||
15 | CONNECT_ERROR: "connect_error", | ||
16 | CONNECT_TIMEOUT: "connect_timeout", | ||
17 | RECONNECT: "reconnect", | ||
18 | RECONNECT_ATTEMPT: "reconnect_attempt", | ||
19 | RECONNECTING: "reconnecting", | ||
20 | RECONNECT_ERROR: "reconnect_error", | ||
21 | RECONNECT_FAILED: "reconnect_failed", | ||
22 | AUTHENTICATED: "authenticated", | ||
23 | AUTHENTICATION_ERROR: "authentication_error", | ||
24 | }, | ||
25 | |||
26 | init: function (settings, callback) { | ||
27 | if (this.callbacks === undefined) { | ||
28 | this.callbacks = []; | ||
29 | } | ||
30 | |||
31 | this.callbacks.push(callback); | ||
32 | |||
33 | if (this.callbacks.length > 1) { | ||
34 | return; | ||
35 | } | ||
36 | |||
37 | this.createIndicator(); | ||
38 | this.updateIndicator(this.STATUS.NOT_CONNECTED); | ||
39 | |||
40 | var url = settings.control.remote.webcastorUrl; | ||
41 | var channel = settings.control.remote.channelId; | ||
42 | var broadcasterKey = settings.key; | ||
43 | |||
44 | require([url + "/socket.io/socket.io.js"], function (io) { | ||
45 | webcastor.io = io; | ||
46 | webcastor.socket = webcastor.connect(url, channel, broadcasterKey); | ||
47 | |||
48 | for (var i = 0; i < webcastor.callbacks.length; i++) { | ||
49 | webcastor.callbacks[i](); | ||
50 | } | ||
51 | }); | ||
52 | }, | ||
53 | |||
54 | getColorFromStatus: function (status) { | ||
55 | switch (status) { | ||
56 | case this.STATUS.CONNECTED: | ||
57 | return "transparent"; | ||
58 | |||
59 | case this.STATUS.NOT_CONNECTED: | ||
60 | return "yellow"; | ||
61 | |||
62 | case this.STATUS.ERROR: | ||
63 | return "red"; | ||
64 | |||
65 | case this.STATUS.AUTHENTICATED: | ||
66 | return "green"; | ||
67 | |||
68 | case this.STATUS.AUTHENTICATION_ERROR: | ||
69 | return "orange"; | ||
70 | } | ||
71 | }, | ||
72 | |||
73 | getStatusFromEvent: function (event) { | ||
74 | switch (event) { | ||
75 | case this.EVENT.CONNECT: | ||
76 | case this.EVENT.RECONNECT: | ||
77 | return this.STATUS.CONNECTED; | ||
78 | |||
79 | case this.EVENT.RECONNECTING: | ||
80 | return this.STATUS.NOT_CONNECTED; | ||
81 | |||
82 | case this.EVENT.CONNECT_ERROR: | ||
83 | case this.EVENT.CONNECT_TIMEOUT: | ||
84 | case this.EVENT.RECONNECT_ATTEMPT: | ||
85 | case this.EVENT.RECONNECT_ERROR: | ||
86 | case this.EVENT.RECONNECT_FAILED: | ||
87 | case this.EVENT.AUTHENTICATION_ERROR: | ||
88 | return this.STATUS.ERROR; | ||
89 | |||
90 | case this.EVENT.AUTHENTICATED: | ||
91 | return this.STATUS.AUTHENTICATED; | ||
92 | |||
93 | case this.EVENT.AUTHENTICATION_ERROR: | ||
94 | return this.STATUS.AUTHENTICATION_ERROR; | ||
95 | } | ||
96 | }, | ||
97 | |||
98 | createIndicator: function () { | ||
99 | this.indicator = document.createElement("s-indicator"); | ||
100 | |||
101 | this.indicator.style.width = "1px"; | ||
102 | this.indicator.style.height = "1px"; | ||
103 | this.indicator.style.position = "absolute"; | ||
104 | this.indicator.style.top = "-1px"; | ||
105 | this.indicator.style.right = "-1px"; | ||
106 | this.indicator.style.transition = "box-shadow 1s"; | ||
107 | |||
108 | document.body.appendChild(this.indicator); | ||
109 | }, | ||
110 | |||
111 | updateIndicator: function (status) { | ||
112 | this.indicator.style.boxShadow = "0 0 40px 15px " + this.getColorFromStatus(status); | ||
113 | }, | ||
114 | |||
115 | bindStatusEvent: function (event, status) { | ||
116 | this.socket.on(event, function (eventDetail) { | ||
117 | webcastor.updateIndicator(status); | ||
118 | }); | ||
119 | }, | ||
120 | |||
121 | bindStatusEvents: function () { | ||
122 | for (var event in this.EVENT) { | ||
123 | var eventValue = this.EVENT[event]; | ||
124 | this.bindStatusEvent(eventValue, this.getStatusFromEvent(eventValue)); | ||
125 | } | ||
126 | }, | ||
127 | |||
128 | connect: function (url, channel, broadcasterKey) { | ||
129 | if (this.socket !== undefined) { | ||
130 | return this.socket; | ||
131 | } | ||
132 | |||
133 | this.socket = this.io.connect(url, { | ||
134 | "query": "channel=" + channel + (broadcasterKey !== undefined ? "&password=" + broadcasterKey : ""), | ||
135 | "force new connection": true, | ||
136 | }); | ||
137 | |||
138 | this.bindStatusEvents(); | ||
139 | |||
140 | return this.socket; | ||
141 | }, | ||
142 | |||
143 | on: function (event, handler) { | ||
144 | this.socket.on(event, function (eventDetail) { | ||
145 | handler(eventDetail); | ||
146 | }); | ||
147 | }, | ||
148 | |||
149 | send: function (message) { | ||
150 | this.socket.send(message); | ||
151 | }, | ||
152 | }; | ||
153 | |||
154 | return webcastor; | ||
155 | |||
156 | }); | ||