pokemon-showdown/dev-tools/users-utils.js
Guangcong Luo 095652c9ee Exit process when child process exits
If a child process dies in a way that it can't recover from, the main
process now exits, to prevent loops of 'Error: channel closed'
thousands of times a second, filling up the error log and making it
hard to track down the real error.
2016-02-03 22:40:51 -06:00

38 lines
1.0 KiB
JavaScript

'use strict';
let EventEmitter = require('events').EventEmitter;
function createWorker() {
let fakeWorker = new EventEmitter();
fakeWorker.send = function () {};
fakeWorker.process = {connected: true};
Sockets.workers[fakeWorker.id] = fakeWorker;
return fakeWorker;
}
function createConnection(ip, workerid, socketid) {
if (!workerid || !socketid) {
workerid = Object.keys(Sockets.workers)[0];
if (!workerid) workerid = createWorker().id;
socketid = 1;
while (Users.connections.has(workerid + '-' + socketid)) {
socketid++;
}
}
let connectionid = workerid + '-' + socketid;
let connection = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
Users.connections.set(connectionid, connection);
return connection;
}
function createUser(connection) {
if (!connection) connection = createConnection();
let user = new Users.User(connection);
connection.user = user;
user.joinRoom('global', connection);
return user;
}
exports.Connection = createConnection;
exports.User = createUser;