eval-server/index.js
ChomeNS b13456c9c8 use worker threads
getPlayerList will temporailly not work for now because i am too lazy to
fix the output thing
2023-08-18 08:58:54 +07:00

51 lines
1.2 KiB
JavaScript

const { VM } = require('vm2')
const { Server } = require('socket.io')
const util = require('util')
const { stylize } = require('./colors')
const { Worker } = require('worker_threads')
const path = require('path')
const io = new Server(3069)
io.on('connection', (socket) => {
let worker
function reset () {
worker = new Worker(path.join(__dirname, 'vm.js'))
worker.on('message', (msg) => {
switch (msg.type) {
case 'socketEmit':
socket.emit(...msg.data)
break
case 'socketOnce':
socket.once(...msg.data)
break
}
})
}
reset()
socket.on('setFunctions', (jsonArray) => {
worker.postMessage({ type: 'setFunctions', jsonArray })
})
socket.on('runCode', (transactionId, code) => {
worker.postMessage({ type: 'runCode', code })
worker.on('message', ({ type, error, output }) => {
if (type !== 'codeOutput') return
socket.emit('codeOutput', transactionId, error, output)
})
})
socket.on('reset', reset)
})
process.on('uncaughtException', (e) => {
console.log(`Caught an uncaught exception!\n${e.stack}`)
})