-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.js
82 lines (67 loc) · 3.22 KB
/
server.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
require('dotenv').config();
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const { TikTokConnectionWrapper, getGlobalConnectionCount } = require('./connectionWrapper');
const app = express();
const httpServer = createServer(app);
// Enable cross origin resource sharing
const io = new Server(httpServer, {
cors: {
origin: '*'
}
});
io.on('connection', (socket) => {
let tiktokConnectionWrapper;
socket.on('setUniqueId', (uniqueId, options) => {
// Prohibit the client from specifying these options (for security reasons)
if (typeof options === 'object') {
delete options.requestOptions;
delete options.websocketOptions;
}
// Is the client already connected to a stream? => Disconnect
if (tiktokConnectionWrapper) {
tiktokConnectionWrapper.disconnect();
}
// Connect to the given username (uniqueId)
try {
tiktokConnectionWrapper = new TikTokConnectionWrapper(uniqueId, options, true);
tiktokConnectionWrapper.connect();
} catch(err) {
socket.emit('disconnected', err.toString());
return;
}
// Redirect wrapper control events once
tiktokConnectionWrapper.once('connected', state => socket.emit('tiktokConnected', state));
tiktokConnectionWrapper.once('disconnected', reason => socket.emit('tiktokDisconnected', reason));
// Notify client when stream ends
tiktokConnectionWrapper.connection.on('streamEnd', () => socket.emit('streamEnd'));
// Redirect message events
tiktokConnectionWrapper.connection.on('roomUser', msg => socket.emit('roomUser', msg));
tiktokConnectionWrapper.connection.on('member', msg => socket.emit('member', msg));
tiktokConnectionWrapper.connection.on('chat', msg => socket.emit('chat', msg));
tiktokConnectionWrapper.connection.on('gift', msg => socket.emit('gift', msg));
tiktokConnectionWrapper.connection.on('social', msg => socket.emit('social', msg));
tiktokConnectionWrapper.connection.on('like', msg => socket.emit('like', msg));
tiktokConnectionWrapper.connection.on('questionNew', msg => socket.emit('questionNew', msg));
tiktokConnectionWrapper.connection.on('linkMicBattle', msg => socket.emit('linkMicBattle', msg));
tiktokConnectionWrapper.connection.on('linkMicArmies', msg => socket.emit('linkMicArmies', msg));
tiktokConnectionWrapper.connection.on('liveIntro', msg => socket.emit('liveIntro', msg));
tiktokConnectionWrapper.connection.on('subscribe', msg => socket.emit('subscribe', msg)); // sorry zerody lmao
});
socket.on('disconnect', () => {
if(tiktokConnectionWrapper) {
tiktokConnectionWrapper.disconnect();
}
});
});
// Emit global connection statistics
setInterval(() => {
io.emit('statistic', { globalConnectionCount: getGlobalConnectionCount() });
}, 5000)
// Serve frontend files
app.use(express.static('public'));
// Start http listener
const port = process.env.PORT || 8082;
httpServer.listen(port);
console.info(`Server running! Widget is available at http://localhost:${port} as of now!`);