-
Notifications
You must be signed in to change notification settings - Fork 4
/
s.js
208 lines (190 loc) · 5.98 KB
/
s.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Lazy's Live Websocket Server
Copyright 2022 Lazy All rights reserved.
*/
const WebSocketServer = require("ws").Server;
var http = require("http");
var url = require("url");
var rp = require("request-promise");
const cfg = {
port: 8848,
//ssl_key: process.cwd()+'/s1.imlazy.ink.key',
//ssl_cert: process.cwd()+'/s1.imlazy.ink_bundle.crt'
};
const processRequest = (req, res) => {
res.writeHead(200);
res.end("Lazy's Live Websocket Server | reference: https://live.lazy.ink");
};
//const app = https.createServer({
// 向server传递key和cert参数
// key: fs.readFileSync(cfg.ssl_key),
// cert: fs.readFileSync(cfg.ssl_cert)
//}, processRequest).listen(cfg.port);
//const wss = new WebSocketServer({
// server: app
//});
const app = http
.createServer(
{
// 向server传递key和cert参数
//key: fs.readFileSync(cfg.ssl_key),
//cert: fs.readFileSync(cfg.ssl_cert)
},
processRequest
)
.listen(cfg.port);
const ws = new WebSocketServer({
server: app,
});
//const server = ws.createServer(options,function (connect){
//连接上来的时候
//wss.on('connection', (connect) => {
console.log(`
██╗ █████╗ ███████╗██╗ ██╗ ██ ███████╗ ██╗ ██╗██╗ ██╗███████╗
██║ ██╔══██╗╚══███╔╝╚██╗ ██╔╝ ██ ██╔════╝ ██║ ██║██║ ██║██╔════╝
██║ ███████║ ███╔╝ ╚████╔╝ ███████╗ ██║ ██║██║ ██║█████╗
██║ ██╔══██║ ███╔╝ ╚██╔╝ ╚════██║ ██║ ██║╚██╗ ██╔╝██╔══╝
███████╗██║ ██║███████╗ ██║ ███████║ ███████╗██║ ╚████╔╝ ███████╗
╚══════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝ ╚══════╝╚═╝ ╚═══╝ ╚══════╝
`)
console.log(" + Copyright 2022 Lazy All rights reserved\n + Version 2.0")
console.log("[WELCOME] 欢迎使用 Lazy's Live 聊天服务");
console.log("[INFO] 服务已启动,端口:"+cfg.port);
ws.on("connection", (connect, reqData) => {
var url233 = url.parse(reqData.url, true).query;
var ok = JSON.parse(JSON.stringify(url233));
if (!(ok.accessToken || ok.clientToken)) {
console.log("用户未认证,令牌为空");
connect.terminate();
return;
}
//身份验证
var options = {
method: "POST",
uri: "https://skin.fxcraft.cn/api/yggdrasil/authserver/validate",
body: {
accessToken: ok.accessToken,
clientToken: ok.clientToken,
},
json: true,
};
rp(options)
.then(function (suC) {
console.log("认证成功,令牌有效");
})
.catch(function (err) {
console.log(err);
console.log("错误的令牌");
connect.terminate();
console.log(JSON.parse(url233));
});
connect.on("message", (str) => {
if (typeof str == "string") {
try {
let data = JSON.parse(str);
console.log("是字符串:" + data);
return;
} catch (e) {
return;
}
} else {
try {
let data = JSON.parse(str);
//console.log("是数组");
} catch (e) {
console.log("捕获到一条野生消息,已阻止");
//console.log(e)
return;
}
}
let data = JSON.parse(str);
console.log(data);
switch (data.type) {
case "setName":
connect.nickname = data.nickname;
clear_name = data.nickname
.replace(/</g, "<")
.replace(/>/g, ">");
boardcast(
JSON.stringify({
type: "serverInformation",
// 发送xxx进入直播间弹幕
message: clear_name + "进入直播间",
})
);
boardcast(
JSON.stringify({
type: "chatterList",
list: getAllChatter(),
})
);
break;
case "chat":
connect.message = data.message;
var clear_msg = data.message
.replace(/</g, "<")
.replace(/>/g, ">");
//clear_name = connect.nickname
//.replace(/</g, "<")
//.replace(/>/g, ">");
boardcast(
JSON.stringify({
type: "chat",
//ame: connect.nickname,
name: clear_name,
message: clear_msg,
})
);
break;
default:
break;
}
});
//关闭链接的时候
connect.on("close", () => {
//离开房间
var clear_name = connect.nickname
.replace(/</g, "<")
.replace(/>/g, ">");
boardcast(
JSON.stringify({
type: "serverInformation",
message: clear_name + "离开直播间",
})
);
//从在线聊天的人数上面除去
boardcast(
JSON.stringify({
type: "chatterList",
list: getAllChatter(),
})
);
});
//错误处理
connect.on("error", (err) => {
console.log(err);
});
}); //.listen(8848,"127.0.0.1")
//封装发送消息的函数(向每个链接的用户发送消息)
const boardcast = (str) => {
console.log(str);
//wss.connection.forEach((connect)=>{
ws.clients.forEach(function each(connect) {
connect.send(str);
});
//wss.connections.forEach(function each(connect){
//connect.sendText(str)
// connect.send(str)
//})
};
//封装获取所有聊天者的nickname
const getAllChatter = () => {
let chartterArr = [];
//wss.connections.forEach((connect)=>{
// chartterArr.push({name:connect.nickname})
//});
ws.clients.forEach(function each(connect) {
chartterArr.push({ name: connect.nickname });
});
return chartterArr;
};