-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
116 lines (95 loc) · 3.67 KB
/
test.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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const { WebcastPushConnection } = require('tiktok-live-connector');
// 初始化 Express 应用
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: 'http://localhost:3000', // 允许前端的请求地址
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type'],
credentials: true
},
transports: ['websocket'] // 强制只使用 WebSocket
});
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
// 定义 TikTok 用户名
let tiktokLiveConnection = null;
// 监听来自前端的 connectToTikTok 事件
socket.on('connectToTikTok', (tiktokUsername) => {
console.log(`Connecting to TikTok live stream of ${tiktokUsername}`);
// 如果已经有连接,断开之前的连接
if (tiktokLiveConnection) {
tiktokLiveConnection.disconnect();
console.log('Previous connection closed.');
}
tiktokLiveConnection = new WebcastPushConnection(tiktokUsername);
// 连接到 TikTok 直播间
tiktokLiveConnection.connect().then(state => {
console.info(`Connected to roomId ${state.roomId} for ${tiktokUsername}`);
// 处理实时消息 (chat)
tiktokLiveConnection.on('chat', data => {
console.log(`${data.uniqueId}(userId:${data.userId}) writes: ${data.comment}`);
// 通过 Socket.IO 将消息发送给前端
socket.emit('chatMessage', {
userpic:data.profilePictureUrl,
username: data.uniqueId,
comment: data.comment,
time: new Date().toLocaleString()
});
});
// 处理点赞 (like)
tiktokLiveConnection.on('like', data => {
console.log(`${data.uniqueId} sent ${data.likeCount} likes, total likes: ${data.totalLikeCount}`);
// 通过 Socket.IO 将点赞数据发送给前端
socket.emit('likeMessage', {
userpic:data.profilePictureUrl,
username: data.uniqueId,
likeCount: data.likeCount,
totalLikes: data.totalLikeCount,
time: new Date().toLocaleString()
});
});
// 处理礼物 (gift)
tiktokLiveConnection.on('gift', data => {
console.log(`${data.uniqueId} (userId:${data.userId}) sends ${data.giftId}`);
// 通过 Socket.IO 将礼物数据发送给前端
socket.emit('giftMessage', {
userpic:data.profilePictureUrl,
username: data.uniqueId,
//giftId: data.giftId,
giftName:data.describe,
time: new Date().toLocaleString()
});
});
}).catch(err => {
console.error('Failed to connect to TikTok:', err);
socket.emit('connectionError', `Failed to connect to ${tiktokUsername}`);
});
// 处理连接错误
tiktokLiveConnection.on('error', (err) => {
console.error('Error from TikTok connection:', err);
socket.emit('connectionError', `Connection error for ${tiktokUsername}`);
});
});
socket.on('disconnectTikTok', () => {
if (tiktokLiveConnection) {
tiktokLiveConnection.disconnect(); // 断开当前 TikTok 连接
console.log('TikTok live connection closed.');
}
});
socket.on('disconnect', () => {
console.log(`Client ${socket.id} disconnected`);
if (tiktokLiveConnection) {
tiktokLiveConnection.disconnect();
console.log('Live connection closed.');
}
});
});
// 启动服务器
server.listen(3001, () => {
console.log('Server is running on port 3001');
});