-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.js
87 lines (76 loc) · 3.1 KB
/
Chat.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
const net = require('net')
const xmljs = require('xml-js')
const { workerData, parentPort } = require('node:worker_threads')
const utils = require('./utils')
function textFix (xml) {
for (const [key, value] of Object.entries(xml)) {
if (Object.prototype.hasOwnProperty.call(value, '_text')) {
xml[key] = value._text
} else {
xml[key] = textFix(value)
}
}
return (xml)
}
class Chat {
constructor () {
const self = this
this.clients = []
this.config = workerData.config
// Set global for logger
if (this.config.chatHistoryFile && this.config.chatHistoryFile !== '') global.chatHistoryFile = this.config.chatHistoryFile
parentPort.on('message', async (value) => {
if (value.exit) {
for (const client of this.clients) {
await client.socket.destroy()
}
this.server.close(function () {
self.server.unref()
})
process.exit(0)
}
})
}
broadcast (payload, rawData) {
if (this.config.chatDiscordWebhook && this.config.chatDiscordWebhook !== '') {
fetch(this.config.chatDiscordWebhook, {
method: 'POST',
body: JSON.stringify({ username: payload.racine.pseudo, content: payload.racine.msg })
})
}
for (const client of this.clients) {
client.socket.write(rawData + '\0')
}
}
run () {
const self = this
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
this.server = net.createServer(function (socket) {
// We have a connection - a socket object is assigned to the connection automatically
utils.logger('chat', `Client ${socket.remoteAddress} connected, assigned port ${socket.remotePort}`)
self.clients.push({ socket, ip: socket.remoteAddress, port: socket.remotePort })
// Add a 'data' event handler to this instance of socket
socket.on('data', function (rawData) {
const payload = textFix(xmljs.xml2js(rawData.toString().replace('\0', ''), { compact: true }))
if (!Object.prototype.hasOwnProperty.call(payload, 'who')) {
utils.logger('chat', `${payload.racine.pseudo}: ${payload.racine.msg}`)
self.broadcast(payload, rawData)
}
})
// Add a 'close' event handler to this instance of socket
socket.on('close', function (data) {
utils.logger('chat', `Client ${socket.remoteAddress} disconnected, with port ${socket.remotePort}`)
self.clients.splice(self.clients.findIndex((client) => client.socket === socket), 1)
})
socket.on('error', function (err) {
utils.logger('chat', `Client ${socket.remoteAddress} unexpectedly closed, with error ${err}`)
self.clients.splice(self.clients.findIndex((client) => client.socket === socket), 1)
})
}).listen(this.config.chatPort, '127.0.0.1')
utils.logger('chat', `Chat Server listening on ${this.config.publicIP}:${this.config.chatPort}...`)
}
}
const chatServer = new Chat()
chatServer.run()