const fs = require('fs'); const https = require('https'); const WebSocket = require('ws'); const { parse, stringify } = require('flatted/cjs'); module.exports = { name: "Websockets Server (8080)", init: (dclient) => { const server = https.createServer({ cert: fs.readFileSync('./cert.pem'), key: fs.readFileSync('./privkey.pem') }); const wss = new WebSocket.Server({ server }); function noop() { } function heartbeat() { this.isAlive = true; } wss.on('connection', function connection(ws) { ws.isAlive = true; ws.on('message', function incoming(message) { if (message == "name") ws.send(stringify( { type: 'starterpack', user: { avatar: dclient.user.avatar, avatarURL: dclient.user.avatarURL, bot: dclient.user.bot, // client: dclient.user.client, createdAt: dclient.user.createdAt, createdTimestamp: dclient.user.createdTimestamp, defaultAvatarURL: dclient.user.defaultAvatarURL, discriminator: dclient.user.discriminator, displayAvatarURL: dclient.user.displayAvatarURL, id: dclient.user.id, //lastMessage: dclient.user.lastMessage, lastMessageID: dclient.user.lastMessageID, presence: dclient.user.presence, tag: dclient.user.tag, username: dclient.user.username, verified: dclient.user.verified } })) }); ws.on('pong', heartbeat); dclient.on('message', (message) => { wss.clients.forEach(function each(client) { if (client !== ws && client.readyState === WebSocket.OPEN) { client.send(stringify(message)) } }) }) // ws.send('something'); }); const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { if (ws.isAlive === false) return ws.terminate(); ws.isAlive = false; ws.ping(noop); }); }, 5000); server.listen(8080); } }