Compare commits

..

No commits in common. "master" and "23e42d86410025fc992ca7edb8842c0b87f3a890" have entirely different histories.

11 changed files with 24 additions and 251 deletions

View File

@ -1,58 +1,10 @@
"use strict"; "use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true; exports.__esModule = true;
exports.Channel = void 0; exports.Channel = void 0;
var chalk = require("chalk"); var Channel = /** @class */ (function () {
var events_1 = require("events"); function Channel(_id) {
var Logger_1 = require("./Logger"); this._id = _id;
var Channel = /** @class */ (function (_super) {
__extends(Channel, _super);
function Channel(_id, server) {
var _this = _super.call(this) || this;
_this._id = _id;
_this.connectedClients = [];
_this.server = server;
_this.logger = new Logger_1.Logger(_id, chalk.yellow);
_this.server.channels.push(_this);
return _this;
} }
Channel.prototype.bindEventListeners = function () {
var _this = this;
this.on('a', function (msg) {
if (!msg.message)
return;
_this.logger.log("Received chat message: " + msg.message);
_this.connectedClients.forEach(function (cl) {
cl.sendArray([{
m: 'a',
a: msg.message
}]);
});
});
};
Channel.prototype.connectClient = function (cl) {
if (this.hasClient(cl)) {
}
else {
this.connectedClients.push(cl);
cl.sendChannelMessage(this);
}
cl.channel = this;
};
Channel.prototype.isLobby = function () { Channel.prototype.isLobby = function () {
if (this._id == 'lobby' || this._id.startsWith('lobby') && !isNaN(parseInt(this._id.substr(5, 2)))) if (this._id == 'lobby' || this._id.startsWith('lobby') && !isNaN(parseInt(this._id.substr(5, 2))))
return true; return true;
@ -60,26 +12,6 @@ var Channel = /** @class */ (function (_super) {
return true; return true;
return false; return false;
}; };
Channel.prototype.hasClient = function (cl) {
for (var _i = 0, _a = this.connectedClients; _i < _a.length; _i++) {
var c = _a[_i];
if (cl.id == c.id) {
return true;
}
}
return false;
};
Channel.prototype.getIncrementedIDNumber = function () {
var num;
var gotNum = parseInt(this._id.substr(5));
var idHasNum = typeof gotNum == 'number';
if (idHasNum) {
return gotNum++;
}
else {
return 1;
}
};
return Channel; return Channel;
}(events_1.EventEmitter)); }());
exports.Channel = Channel; exports.Channel = Channel;

View File

@ -19,13 +19,11 @@ exports.Client = void 0;
var events_1 = require("events"); var events_1 = require("events");
var Client = /** @class */ (function (_super) { var Client = /** @class */ (function (_super) {
__extends(Client, _super); __extends(Client, _super);
function Client(id, ws, server) { function Client(ws, server) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this.id = id;
_this.server = server; _this.server = server;
_this.ws = ws; _this.ws = ws;
_this.bindEventListeners(); _this.bindEventListeners();
_this.server.setClientChannel(_this, "lobby");
return _this; return _this;
} }
Client.prototype.bindEventListeners = function () { Client.prototype.bindEventListeners = function () {
@ -50,7 +48,7 @@ var Client = /** @class */ (function (_super) {
this.on('a', function (msg) { this.on('a', function (msg) {
if (!msg.message) if (!msg.message)
return; return;
_this.receiveChatMessage(msg); _this.sendChatMessage(msg);
}); });
// TODO user data, other messages, maybe cursors // TODO user data, other messages, maybe cursors
}; };
@ -58,19 +56,11 @@ var Client = /** @class */ (function (_super) {
var jmsgs = JSON.stringify(arr); var jmsgs = JSON.stringify(arr);
this.ws.send(jmsgs); this.ws.send(jmsgs);
}; };
Client.prototype.receiveChatMessage = function (msg) { Client.prototype.sendChatMessage = function (msg) {
// console.log(`Received chat message: ${msg.message}`); console.log("Received chat message: " + msg.message);
// this.server.wsh.clients.forEach((cl, id) => { this.server.wsh.clients.forEach(function (cl, id) {
// cl.sendArray([{m:'a', a: msg.message}]) cl.sendArray([{ m: 'a', a: msg.message }]);
// }); });
this.channel.emit('a', msg);
};
Client.prototype.sendChannelMessage = function (ch) {
this.sendArray([{
m: "ch"
}]);
};
Client.prototype.setChannel = function (str) {
}; };
return Client; return Client;
}(events_1.EventEmitter)); }(events_1.EventEmitter));

View File

@ -21,14 +21,12 @@ var events_1 = require("events");
var Logger_1 = require("./Logger"); var Logger_1 = require("./Logger");
var WebSocketHandler_1 = require("./WebSocketHandler"); var WebSocketHandler_1 = require("./WebSocketHandler");
var WebServer_1 = require("./WebServer"); var WebServer_1 = require("./WebServer");
var Channel_1 = require("./Channel");
var PORT = process.env.PORT; var PORT = process.env.PORT;
var Server = /** @class */ (function (_super) { var Server = /** @class */ (function (_super) {
__extends(Server, _super); __extends(Server, _super);
function Server() { function Server() {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this.bindEventListeners(); _this.bindEventListeners();
_this.channels = [];
_this.logger = new Logger_1.Logger('server', chalk.green); _this.logger = new Logger_1.Logger('server', chalk.green);
_this.logger.debug("port: " + PORT); _this.logger.debug("port: " + PORT);
_this.wsh = new WebSocketHandler_1.WebSocketHandler(_this); _this.wsh = new WebSocketHandler_1.WebSocketHandler(_this);
@ -43,29 +41,6 @@ var Server = /** @class */ (function (_super) {
_this.logger.log('Server started.'); _this.logger.log('Server started.');
}); });
}; };
Server.prototype.setClientChannel = function (cl, str) {
// TODO put client in channel or create new channel
var channelExists = false;
var ch;
for (var _i = 0, _a = this.channels; _i < _a.length; _i++) {
var c = _a[_i];
if (str == c._id)
channelExists = true;
ch = c;
}
if (!channelExists) {
ch = new Channel_1.Channel(str, this);
}
if (ch.hasClient(cl))
return;
if (ch.isLobby() && ch.connectedClients.length >= 20) {
var new_id_1 = ch._id + ch.getIncrementedIDNumber();
ch = this.channels.find(function (ch) { return new_id_1 == str; });
if (!ch)
ch = new Channel_1.Channel(new_id_1, this);
}
ch.connectClient(cl);
};
return Server; return Server;
}(events_1.EventEmitter)); }(events_1.EventEmitter));
exports.Server = Server; exports.Server = Server;

View File

@ -29,7 +29,7 @@ var WebServer = /** @class */ (function (_super) {
_this.bindEventListeners(); _this.bindEventListeners();
_this.app = express(); _this.app = express();
_this.app.use(express.static((0, path_1.join)(process.cwd(), 'dist'))); _this.app.use(express.static((0, path_1.join)(process.cwd(), 'dist')));
_this.server = _this.app.listen(port, function () { }); _this.server = _this.app.listen(port, '0.0.0.0', function () { });
_this.emit('start'); _this.emit('start');
return _this; return _this;
} }

View File

@ -45,8 +45,7 @@ var WebSocketHandler = /** @class */ (function (_super) {
noServer: true noServer: true
}); });
_this.wss.on('connection', function (ws, req) { _this.wss.on('connection', function (ws, req) {
_this.clients.set(nextClientID, new Client_1.Client(nextClientID, ws, _this.server)); _this.clients.set(nextClientID++, new Client_1.Client(ws, _this.server));
nextClientID++;
}); });
_this.wss.on('close', function () { _this.wss.on('close', function () {
_this.logger.warn('WebSocket server closed.'); _this.logger.warn('WebSocket server closed.');

View File

@ -1,50 +1,11 @@
import chalk = require("chalk");
import { EventEmitter } from "events";
import { Client } from "./Client"; import { Client } from "./Client";
import { Logger } from "./Logger";
import { Server } from "./Server";
class Channel extends EventEmitter { class Channel {
_id: string; _id: string;
connectedClients: Client[]; connectedClients: Client[];
server: Server;
logger: Logger;
constructor (_id: string, server: Server) { constructor (_id: string) { // TODO channels
super();
this._id = _id; this._id = _id;
this.connectedClients = [];
this.server = server;
this.logger = new Logger(_id, chalk.yellow);
this.server.channels.push(this);
}
bindEventListeners() {
this.on('a', msg => {
if (!msg.message) return;
this.logger.log(`Received chat message: ${msg.message}`);
this.connectedClients.forEach(cl => {
cl.sendArray([{
m: 'a',
a: msg.message
}]);
});
});
}
connectClient(cl: Client) {
if (this.hasClient(cl)) {
} else {
this.connectedClients.push(cl);
cl.sendChannelMessage(this);
}
cl.channel = this;
} }
isLobby(): boolean { isLobby(): boolean {
@ -52,32 +13,6 @@ class Channel extends EventEmitter {
if (this._id.startsWith('test/') && this._id !== 'test/') return true; if (this._id.startsWith('test/') && this._id !== 'test/') return true;
return false; return false;
} }
hasClient(cl: Client) {
for (let c of this.connectedClients) {
if (cl.id == c.id) {
return true;
}
}
return false;
}
getIncrementedIDNumber(): number {
let num: number;
let gotNum: number = parseInt(this._id.substr(5));
let idHasNum: boolean = typeof gotNum == 'number';
if (idHasNum) {
return gotNum++;
} else {
return 1;
}
}
getPublicInfo() {
}
} }
export { export {

View File

@ -1,25 +1,18 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import WebSocket = require('ws'); import WebSocket = require('ws');
import { Channel } from './Channel';
import { Server } from './Server'; import { Server } from './Server';
class Client extends EventEmitter { class Client extends EventEmitter {
id: string | number;
ws: WebSocket; ws: WebSocket;
server: Server; server: Server;
channel: Channel;
constructor (id: string | number, ws: WebSocket, server: Server) { constructor (ws: WebSocket, server: Server) {
super(); super();
this.id = id;
this.server = server; this.server = server;
this.ws = ws; this.ws = ws;
this.bindEventListeners(); this.bindEventListeners();
this.server.setClientChannel(this, "lobby");
} }
bindEventListeners() { bindEventListeners() {
@ -43,7 +36,7 @@ class Client extends EventEmitter {
this.on('a', msg => { this.on('a', msg => {
if (!msg.message) return; if (!msg.message) return;
this.receiveChatMessage(msg); this.sendChatMessage(msg);
}); });
// TODO user data, other messages, maybe cursors // TODO user data, other messages, maybe cursors
@ -54,25 +47,12 @@ class Client extends EventEmitter {
this.ws.send(jmsgs); this.ws.send(jmsgs);
} }
receiveChatMessage(msg) { // TODO move this to Channel sendChatMessage(msg) { // TODO move this to Channel
// console.log(`Received chat message: ${msg.message}`); console.log(`Received chat message: ${msg.message}`);
// this.server.wsh.clients.forEach((cl, id) => {
// cl.sendArray([{m:'a', a: msg.message}])
// });
this.channel.emit('a', msg);
}
sendChannelMessage(ch: Channel) {
this.sendArray([{
m: "ch",
_id: ch._id
}]);
}
setChannel(str: string) {
this.server.wsh.clients.forEach((cl, id) => {
cl.sendArray([{m:'a', a: msg.message}])
});
} }
} }

View File

@ -5,8 +5,6 @@ import { EventEmitter } from 'events';
import { Logger } from './Logger'; import { Logger } from './Logger';
import { WebSocketHandler } from './WebSocketHandler'; import { WebSocketHandler } from './WebSocketHandler';
import { WebServer } from './WebServer'; import { WebServer } from './WebServer';
import { Channel } from './Channel';
import { Client } from './Client';
const PORT = process.env.PORT; const PORT = process.env.PORT;
@ -14,15 +12,12 @@ class Server extends EventEmitter {
logger: Logger; logger: Logger;
wsh: WebSocketHandler; wsh: WebSocketHandler;
ws: WebServer; ws: WebServer;
channels: Channel[];
constructor () { constructor () {
super(); super();
this.bindEventListeners(); this.bindEventListeners();
this.channels = [];
this.logger = new Logger('server', chalk.green); this.logger = new Logger('server', chalk.green);
this.logger.debug(`port: ${PORT}`); this.logger.debug(`port: ${PORT}`);
this.wsh = new WebSocketHandler(this); this.wsh = new WebSocketHandler(this);
@ -38,34 +33,6 @@ class Server extends EventEmitter {
this.logger.log('Server started.'); this.logger.log('Server started.');
}); });
} }
setClientChannel(cl: Client, str: string) {
// TODO put client in channel or create new channel
let channelExists = false;
let ch: Channel;
for (let c of this.channels) {
if (str == c._id) channelExists = true;
ch = c;
}
if (!channelExists) {
ch = new Channel(str, this);
}
if (ch.hasClient(cl)) return;
if (ch.isLobby() && ch.connectedClients.length >= 20) {
let new_id = ch._id + ch.getIncrementedIDNumber();
ch = this.channels.find(ch => new_id == str);
if (!ch) ch = new Channel(new_id, this);
}
ch.connectClient(cl);
}
} }
export { export {

View File

@ -23,7 +23,7 @@ class WebServer extends EventEmitter {
this.app = express(); this.app = express();
this.app.use(express.static(join(process.cwd(), 'dist'))); this.app.use(express.static(join(process.cwd(), 'dist')));
this.server = this.app.listen(port, () => {}); this.server = this.app.listen(port, '0.0.0.0', () => {});
this.emit('start'); this.emit('start');
} }

View File

@ -38,8 +38,7 @@ class WebSocketHandler extends EventEmitter {
}); });
this.wss.on('connection', (ws, req) => { this.wss.on('connection', (ws, req) => {
this.clients.set(nextClientID, new Client(nextClientID, ws, this.server)); this.clients.set(nextClientID++, new Client(ws, this.server));
nextClientID++;
}); });
this.wss.on('close', () => { this.wss.on('close', () => {

View File

@ -74,10 +74,6 @@ import * as $ from "jquery";
chat.receive(msg); chat.receive(msg);
}); });
gClient.on('ch', msg => {
console.log(msg._id);
});
(globalThis as any).app = { (globalThis as any).app = {
client: gClient client: gClient
} }