Compare commits

...

1 Commits

Author SHA1 Message Date
228dec24a3 checkpoint 2021-09-29 15:42:47 -04:00
11 changed files with 251 additions and 24 deletions

View File

@ -1,10 +1,58 @@
"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.Channel = void 0;
var Channel = /** @class */ (function () {
function Channel(_id) {
this._id = _id;
var chalk = require("chalk");
var events_1 = require("events");
var Logger_1 = require("./Logger");
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 () {
if (this._id == 'lobby' || this._id.startsWith('lobby') && !isNaN(parseInt(this._id.substr(5, 2))))
return true;
@ -12,6 +60,26 @@ var Channel = /** @class */ (function () {
return true;
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;
}());
}(events_1.EventEmitter));
exports.Channel = Channel;

View File

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

View File

@ -21,12 +21,14 @@ var events_1 = require("events");
var Logger_1 = require("./Logger");
var WebSocketHandler_1 = require("./WebSocketHandler");
var WebServer_1 = require("./WebServer");
var Channel_1 = require("./Channel");
var PORT = process.env.PORT;
var Server = /** @class */ (function (_super) {
__extends(Server, _super);
function Server() {
var _this = _super.call(this) || this;
_this.bindEventListeners();
_this.channels = [];
_this.logger = new Logger_1.Logger('server', chalk.green);
_this.logger.debug("port: " + PORT);
_this.wsh = new WebSocketHandler_1.WebSocketHandler(_this);
@ -41,6 +43,29 @@ var Server = /** @class */ (function (_super) {
_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;
}(events_1.EventEmitter));
exports.Server = Server;

View File

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

View File

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

View File

@ -1,11 +1,50 @@
import chalk = require("chalk");
import { EventEmitter } from "events";
import { Client } from "./Client";
import { Logger } from "./Logger";
import { Server } from "./Server";
class Channel {
class Channel extends EventEmitter {
_id: string;
connectedClients: Client[];
server: Server;
logger: Logger;
constructor (_id: string) { // TODO channels
constructor (_id: string, server: Server) {
super();
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 {
@ -13,6 +52,32 @@ class Channel {
if (this._id.startsWith('test/') && this._id !== 'test/') return true;
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 {

View File

@ -1,18 +1,25 @@
import { EventEmitter } from 'events';
import WebSocket = require('ws');
import { Channel } from './Channel';
import { Server } from './Server';
class Client extends EventEmitter {
id: string | number;
ws: WebSocket;
server: Server;
channel: Channel;
constructor (ws: WebSocket, server: Server) {
constructor (id: string | number, ws: WebSocket, server: Server) {
super();
this.id = id;
this.server = server;
this.ws = ws;
this.bindEventListeners();
this.server.setClientChannel(this, "lobby");
}
bindEventListeners() {
@ -36,7 +43,7 @@ class Client extends EventEmitter {
this.on('a', msg => {
if (!msg.message) return;
this.sendChatMessage(msg);
this.receiveChatMessage(msg);
});
// TODO user data, other messages, maybe cursors
@ -47,12 +54,25 @@ class Client extends EventEmitter {
this.ws.send(jmsgs);
}
sendChatMessage(msg) { // TODO move this to Channel
console.log(`Received chat message: ${msg.message}`);
receiveChatMessage(msg) { // TODO move this to Channel
// 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,6 +5,8 @@ import { EventEmitter } from 'events';
import { Logger } from './Logger';
import { WebSocketHandler } from './WebSocketHandler';
import { WebServer } from './WebServer';
import { Channel } from './Channel';
import { Client } from './Client';
const PORT = process.env.PORT;
@ -12,12 +14,15 @@ class Server extends EventEmitter {
logger: Logger;
wsh: WebSocketHandler;
ws: WebServer;
channels: Channel[];
constructor () {
super();
this.bindEventListeners();
this.channels = [];
this.logger = new Logger('server', chalk.green);
this.logger.debug(`port: ${PORT}`);
this.wsh = new WebSocketHandler(this);
@ -33,6 +38,34 @@ class Server extends EventEmitter {
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 {

View File

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

View File

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

View File

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