forked from night/betterttv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-client.js
139 lines (108 loc) · 3.29 KB
/
socket-client.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import throttle from 'lodash.throttle';
import SafeEventEmitter from './utils/safe-event-emitter.js';
import debug from './utils/debug.js';
import {getCurrentUser} from './utils/user.js';
const CONNECTION_STATES = {
DISCONNECTED: 0,
CONNECTING: 1,
CONNECTED: 2,
};
const WEBSOCKET_ENDPOINT = 'wss://sockets.betterttv.net/ws';
let socket;
let state = CONNECTION_STATES.DISCONNECTED;
let attempts = 1;
const joinedChannels = [];
function makeSocketChannel(provider, providerId) {
return `${provider}:${providerId}`;
}
class SocketClient extends SafeEventEmitter {
constructor() {
super();
this.connect();
this.broadcastMe = throttle(this.broadcastMe.bind(this), 1000, {leading: false});
}
connect() {
if (state !== CONNECTION_STATES.DISCONNECTED) return;
state = CONNECTION_STATES.CONNECTING;
debug.log('SocketClient: Connecting to server');
socket = new WebSocket(WEBSOCKET_ENDPOINT);
socket.onopen = () => {
debug.log('SocketClient: Connected to server');
state = CONNECTION_STATES.CONNECTED;
attempts = 1;
if (joinedChannels.length) {
joinedChannels.forEach((socketChannel) => {
const [provider, providerId] = socketChannel.split(':');
this.joinChannel(provider, providerId);
});
}
};
socket.onclose = () => {
if (state === CONNECTION_STATES.DISCONNECTED) return;
debug.log('SocketClient: Disconnected from server');
attempts++;
this.reconnect();
};
socket.onerror = socket.onclose;
socket.onmessage = ({data}) => {
let evt;
try {
evt = JSON.parse(data);
} catch (e) {
debug.log('SocketClient: Error parsing message', e);
return;
}
debug.log('SocketClient: Received event', evt);
this.emit(evt.name, evt.data);
};
}
reconnect() {
if (state === CONNECTION_STATES.CONNECTING) return;
state = CONNECTION_STATES.DISCONNECTED;
if (socket) {
try {
socket.close();
} catch (e) {}
socket = null;
}
setTimeout(() => this.connect(), Math.random() * (2 ** attempts - 1) * 30000);
}
send(name, data) {
if (state !== CONNECTION_STATES.CONNECTED) return;
socket.send(
JSON.stringify({
name,
data,
})
);
}
joinChannel(provider, providerId) {
const socketChannel = makeSocketChannel(provider, providerId);
if (!joinedChannels.includes(socketChannel)) {
joinedChannels.push(socketChannel);
}
this.send('join_channel', {name: socketChannel});
this.broadcastMe(provider, providerId);
}
partChannel(provider, providerId) {
const socketChannel = makeSocketChannel(provider, providerId);
if (!joinedChannels.includes(socketChannel)) return;
joinedChannels.splice(joinedChannels.indexOf(socketChannel), 1);
this.send('part_channel', {name: socketChannel});
}
broadcastMe(provider, providerChannelId, providerUserId) {
if (providerUserId == null) {
const currentUser = getCurrentUser();
if (currentUser == null) {
return;
}
providerUserId = currentUser.id;
}
this.send('broadcast_me', {
provider,
providerId: providerUserId,
channel: makeSocketChannel(provider, providerChannelId),
});
}
}
export default new SocketClient();