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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
define(function () {
var webcastor = {
STATUS: {
CONNECTED: "connected",
NOT_CONNECTED: "notConnected",
ERROR: "error",
AUTHENTICATED: "authenticated",
AUTHENTICATION_ERROR: "authenticationError",
},
EVENT: {
CONNECT: "connect",
CONNECT_ERROR: "connect_error",
CONNECT_TIMEOUT: "connect_timeout",
RECONNECT: "reconnect",
RECONNECT_ATTEMPT: "reconnect_attempt",
RECONNECTING: "reconnecting",
RECONNECT_ERROR: "reconnect_error",
RECONNECT_FAILED: "reconnect_failed",
AUTHENTICATED: "authenticated",
AUTHENTICATION_ERROR: "authentication_error",
},
init: function (settings, callback) {
if (this.callbacks === undefined) {
this.callbacks = [];
}
this.callbacks.push(callback);
if (this.callbacks.length > 1) {
return;
}
this.createIndicator();
this.updateIndicator(this.STATUS.NOT_CONNECTED);
var url = settings.control.remote.webcastorUrl;
var channel = settings.control.remote.channelId;
var broadcasterKey = settings.key;
require([url + "/socket.io/socket.io.js"], function (io) {
webcastor.io = io;
webcastor.socket = webcastor.connect(url, channel, broadcasterKey);
for (var i = 0; i < webcastor.callbacks.length; i++) {
webcastor.callbacks[i]();
}
});
},
getColorFromStatus: function (status) {
switch (status) {
case this.STATUS.CONNECTED:
return "transparent";
case this.STATUS.NOT_CONNECTED:
return "yellow";
case this.STATUS.ERROR:
return "red";
case this.STATUS.AUTHENTICATED:
return "green";
case this.STATUS.AUTHENTICATION_ERROR:
return "orange";
}
},
getStatusFromEvent: function (event) {
switch (event) {
case this.EVENT.CONNECT:
case this.EVENT.RECONNECT:
return this.STATUS.CONNECTED;
case this.EVENT.RECONNECTING:
return this.STATUS.NOT_CONNECTED;
case this.EVENT.CONNECT_ERROR:
case this.EVENT.CONNECT_TIMEOUT:
case this.EVENT.RECONNECT_ATTEMPT:
case this.EVENT.RECONNECT_ERROR:
case this.EVENT.RECONNECT_FAILED:
case this.EVENT.AUTHENTICATION_ERROR:
return this.STATUS.ERROR;
case this.EVENT.AUTHENTICATED:
return this.STATUS.AUTHENTICATED;
case this.EVENT.AUTHENTICATION_ERROR:
return this.STATUS.AUTHENTICATION_ERROR;
}
},
createIndicator: function () {
this.indicator = document.createElement("s-indicator");
this.indicator.style.width = "1px";
this.indicator.style.height = "1px";
this.indicator.style.position = "absolute";
this.indicator.style.top = "-1px";
this.indicator.style.right = "-1px";
this.indicator.style.transition = "box-shadow 1s";
document.body.appendChild(this.indicator);
},
updateIndicator: function (status) {
this.indicator.style.boxShadow = "0 0 40px 15px " + this.getColorFromStatus(status);
},
bindStatusEvent: function (event, status) {
this.socket.on(event, function (eventDetail) {
webcastor.updateIndicator(status);
});
},
bindStatusEvents: function () {
for (var event in this.EVENT) {
var eventValue = this.EVENT[event];
this.bindStatusEvent(eventValue, this.getStatusFromEvent(eventValue));
}
},
connect: function (url, channel, broadcasterKey) {
if (this.socket !== undefined) {
return this.socket;
}
this.socket = this.io.connect(url, {
"query": "channel=" + channel + (broadcasterKey !== undefined ? "&password=" + broadcasterKey : ""),
"force new connection": true,
});
this.bindStatusEvents();
return this.socket;
},
on: function (event, handler) {
this.socket.on(event, function (eventDetail) {
handler(eventDetail);
});
},
send: function (message) {
this.socket.send(message);
},
};
return webcastor;
});
|