|
@@ -1,77 +1,13 @@
|
|
|
-import { IRealm } from "./models/realm";
|
|
|
-
|
|
|
import express from "express";
|
|
|
import http from "http";
|
|
|
import https from "https";
|
|
|
-
|
|
|
import { Server } from "net";
|
|
|
-import { Api } from "./api";
|
|
|
-import defaultConfig, { IConfig } from "./config";
|
|
|
-import { MessageHandler } from "./messageHandler";
|
|
|
-import { IClient } from "./models/client";
|
|
|
-import { IMessage } from "./models/message";
|
|
|
-import { Realm } from "./models/realm";
|
|
|
-import { CheckBrokenConnections } from "./services/checkBrokenConnections";
|
|
|
-import { IMessagesExpire, MessagesExpire } from "./services/messagesExpire";
|
|
|
-import { IWebSocketServer, WebSocketServer } from "./services/webSocketServer";
|
|
|
-
|
|
|
-const init = ({ app, server, options }: {
|
|
|
- app: express.Express,
|
|
|
- server: Server,
|
|
|
- options: IConfig;
|
|
|
-}) => {
|
|
|
- const config = options;
|
|
|
- const realm: IRealm = new Realm();
|
|
|
- const messageHandler = new MessageHandler(realm);
|
|
|
- const api = Api({ config, realm, messageHandler });
|
|
|
-
|
|
|
- const messagesExpire: IMessagesExpire = new MessagesExpire({ realm, config, messageHandler });
|
|
|
- const checkBrokenConnections = new CheckBrokenConnections({
|
|
|
- realm,
|
|
|
- config,
|
|
|
- onClose: (client: IClient) => {
|
|
|
- app.emit("disconnect", client);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- app.use(options.path, api);
|
|
|
-
|
|
|
- const wss: IWebSocketServer = new WebSocketServer({
|
|
|
- server,
|
|
|
- realm,
|
|
|
- config
|
|
|
- });
|
|
|
-
|
|
|
- wss.on("connection", (client: IClient) => {
|
|
|
- const messageQueue = realm.getMessageQueueById(client.getId());
|
|
|
-
|
|
|
- if (messageQueue) {
|
|
|
- let message: IMessage | undefined;
|
|
|
-
|
|
|
- while (message = messageQueue.readMessage()) {
|
|
|
- messageHandler.handle(client, message);
|
|
|
- }
|
|
|
- realm.clearMessageQueue(client.getId());
|
|
|
- }
|
|
|
-
|
|
|
- app.emit("connection", client);
|
|
|
- });
|
|
|
-
|
|
|
- wss.on("message", (client: IClient, message: IMessage) => {
|
|
|
- app.emit("message", client, message);
|
|
|
- messageHandler.handle(client, message);
|
|
|
- });
|
|
|
|
|
|
- wss.on("close", (client: IClient) => {
|
|
|
- app.emit("disconnect", client);
|
|
|
- });
|
|
|
-
|
|
|
- wss.on("error", (error: Error) => {
|
|
|
- app.emit("error", error);
|
|
|
- });
|
|
|
+import defaultConfig, { IConfig } from "./config";
|
|
|
+import { createInstance } from "./instance";
|
|
|
|
|
|
- messagesExpire.startMessagesExpiration();
|
|
|
- checkBrokenConnections.start();
|
|
|
+type Optional<T> = {
|
|
|
+ [P in keyof T]?: (T[P] | undefined);
|
|
|
};
|
|
|
|
|
|
function ExpressPeerServer(server: Server, options?: IConfig) {
|
|
@@ -92,16 +28,12 @@ function ExpressPeerServer(server: Server, options?: IConfig) {
|
|
|
"can't start PeerServer");
|
|
|
}
|
|
|
|
|
|
- init({ app, server, options: newOptions });
|
|
|
+ createInstance({ app, server, options: newOptions });
|
|
|
});
|
|
|
|
|
|
return app;
|
|
|
}
|
|
|
|
|
|
-type Optional<T> = {
|
|
|
- [P in keyof T]?: (T[P] | undefined);
|
|
|
-};
|
|
|
-
|
|
|
function PeerServer(options: Optional<IConfig> = {}, callback?: (server: Server) => void) {
|
|
|
const app = express();
|
|
|
|
|
@@ -113,11 +45,11 @@ function PeerServer(options: Optional<IConfig> = {}, callback?: (server: Server)
|
|
|
let path = newOptions.path;
|
|
|
const port = newOptions.port;
|
|
|
|
|
|
- if (path[0] !== "/") {
|
|
|
+ if (!path.startsWith('/')) {
|
|
|
path = "/" + path;
|
|
|
}
|
|
|
|
|
|
- if (path[path.length - 1] !== "/") {
|
|
|
+ if (!path.endsWith('/')) {
|
|
|
path += "/";
|
|
|
}
|
|
|
|