chore: add eslint
This commit is contained in:
parent
207e4d521e
commit
ed72f3edca
11 changed files with 4419 additions and 278 deletions
21
.eslintrc.json
Normal file
21
.eslintrc.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
},
|
||||
"extends": [
|
||||
"plugin:react/recommended",
|
||||
"standard"
|
||||
],
|
||||
"overrides": [
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module"
|
||||
},
|
||||
"plugins": [
|
||||
"react"
|
||||
],
|
||||
"rules": {
|
||||
}
|
||||
}
|
4132
package-lock.json
generated
4132
package-lock.json
generated
File diff suppressed because it is too large
Load diff
11
package.json
11
package.json
|
@ -7,7 +7,8 @@
|
|||
"main": "./npx/bitburner-filesync.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node src/index.js"
|
||||
"start": "node src/index.js",
|
||||
"lint": "eslint ./src --fix"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -29,5 +30,13 @@
|
|||
"convict": "^6.2.3",
|
||||
"signal-js": "^3.0.1",
|
||||
"ws": "^8.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^8.24.0",
|
||||
"eslint-config-standard": "^17.0.0",
|
||||
"eslint-plugin-import": "^2.26.0",
|
||||
"eslint-plugin-n": "^15.3.0",
|
||||
"eslint-plugin-promise": "^6.0.1",
|
||||
"eslint-plugin-react": "^7.31.8"
|
||||
}
|
||||
}
|
||||
|
|
150
src/config.js
150
src/config.js
|
@ -1,84 +1,82 @@
|
|||
import convict from "convict";
|
||||
import { existsSync } from "fs";
|
||||
import convict from 'convict'
|
||||
import { existsSync } from 'fs'
|
||||
|
||||
// Define a schema
|
||||
export let config = convict({
|
||||
allowedFiletypes: {
|
||||
doc: 'Filetypes that are synchronized to the game.',
|
||||
format: 'Array',
|
||||
default: [".js", ".script", ".txt"]
|
||||
export const config = convict({
|
||||
allowedFiletypes: {
|
||||
doc: 'Filetypes that are synchronized to the game.',
|
||||
format: 'Array',
|
||||
default: ['.js', '.script', '.txt']
|
||||
},
|
||||
allowDeletingFiles: {
|
||||
doc: 'Allow deleting files in game if they get deleted off disk.',
|
||||
format: 'Boolean',
|
||||
default: false,
|
||||
arg: 'allowDeletingFiles'
|
||||
},
|
||||
port: {
|
||||
doc: 'The port to bind to.',
|
||||
format: 'Number',
|
||||
default: 12525,
|
||||
env: 'BB_PORT',
|
||||
arg: 'port'
|
||||
},
|
||||
scriptsFolder: {
|
||||
doc: 'The to be synchronized folder.',
|
||||
format: 'String',
|
||||
default: '.',
|
||||
env: 'BB_SCRIPTFOLDER',
|
||||
arg: 'folder'
|
||||
},
|
||||
quiet: {
|
||||
doc: 'Log less internal events to stdout.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_VERBOSE',
|
||||
default: false,
|
||||
arg: 'quiet'
|
||||
},
|
||||
dry: {
|
||||
doc: 'Only print the files to be synchronised.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_DRY',
|
||||
default: false,
|
||||
arg: 'dry'
|
||||
},
|
||||
definitionFile: {
|
||||
update: {
|
||||
doc: 'Automatically pull the definition file from the game.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_UPDATE_DEF',
|
||||
default: false
|
||||
},
|
||||
allowDeletingFiles: {
|
||||
doc: 'Allow deleting files in game if they get deleted off disk.',
|
||||
format: 'Boolean',
|
||||
default: false,
|
||||
arg: 'allowDeletingFiles',
|
||||
},
|
||||
port: {
|
||||
doc: 'The port to bind to.',
|
||||
format: 'Number',
|
||||
default: 12525,
|
||||
env: 'BB_PORT',
|
||||
arg: 'port'
|
||||
},
|
||||
scriptsFolder: {
|
||||
doc: 'The to be synchronized folder.',
|
||||
format: 'String',
|
||||
default: '.',
|
||||
env: 'BB_SCRIPTFOLDER',
|
||||
arg: 'folder'
|
||||
},
|
||||
quiet: {
|
||||
doc: 'Log less internal events to stdout.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_VERBOSE',
|
||||
default: false,
|
||||
arg: 'quiet'
|
||||
},
|
||||
dry: {
|
||||
doc: 'Only print the files to be synchronised.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_DRY',
|
||||
default: false,
|
||||
arg: 'dry'
|
||||
},
|
||||
definitionFile: {
|
||||
update: {
|
||||
doc: 'Automatically pull the definition file from the game.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_UPDATE_DEF',
|
||||
default: false
|
||||
},
|
||||
location: {
|
||||
doc: 'Location/name of where the definition file gets placed.',
|
||||
format: 'String',
|
||||
env: 'BB_LOCATION_DEF',
|
||||
default: "./NetScriptDefinitions.d.ts"
|
||||
}
|
||||
},
|
||||
pushAllOnConnection: {
|
||||
doc: 'Push all files when initial connection is made.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_CON_PUSH',
|
||||
default: false,
|
||||
arg: 'pushAllOnConnection'
|
||||
location: {
|
||||
doc: 'Location/name of where the definition file gets placed.',
|
||||
format: 'String',
|
||||
env: 'BB_LOCATION_DEF',
|
||||
default: './NetScriptDefinitions.d.ts'
|
||||
}
|
||||
});
|
||||
},
|
||||
pushAllOnConnection: {
|
||||
doc: 'Push all files when initial connection is made.',
|
||||
format: 'Boolean',
|
||||
env: 'BB_CON_PUSH',
|
||||
default: false,
|
||||
arg: 'pushAllOnConnection'
|
||||
}
|
||||
})
|
||||
|
||||
export function loadConfig() {
|
||||
const configFile = "filesync.json";
|
||||
if (existsSync(configFile)) {
|
||||
try {
|
||||
config.loadFile(configFile);
|
||||
} catch (e) {
|
||||
throw new Error(`Unable to load configuration file at ${configFile}: ${e}`);
|
||||
}
|
||||
} else if (!config.get("quiet")) {
|
||||
console.log("No configuration file found.")
|
||||
export function loadConfig () {
|
||||
const configFile = 'filesync.json'
|
||||
if (existsSync(configFile)) {
|
||||
try {
|
||||
config.loadFile(configFile)
|
||||
} catch (e) {
|
||||
throw new Error(`Unable to load configuration file at ${configFile}: ${e}`)
|
||||
}
|
||||
} else if (!config.get('quiet')) {
|
||||
console.log('No configuration file found.')
|
||||
}
|
||||
|
||||
// Perform validation
|
||||
config.validate({ allowed: 'strict' });
|
||||
// Perform validation
|
||||
config.validate({ allowed: 'strict' })
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
export class EventType {
|
||||
static ConnectionMade = "ConnectionMade";
|
||||
static FileChanged = "FileChanged";
|
||||
static FileDeleted = "FileDeleted";
|
||||
static MessageReceived = "MessageReceived";
|
||||
static MessageSend = "MessageSend";
|
||||
static ConnectionMade = 'ConnectionMade'
|
||||
static FileChanged = 'FileChanged'
|
||||
static FileDeleted = 'FileDeleted'
|
||||
static MessageReceived = 'MessageReceived'
|
||||
static MessageSend = 'MessageSend'
|
||||
}
|
|
@ -1,35 +1,33 @@
|
|||
import CheapWatch from "cheap-watch";
|
||||
import { config } from "./config.js";
|
||||
import { EventType } from "./eventTypes.js";
|
||||
import { resolve } from "path";
|
||||
import CheapWatch from 'cheap-watch'
|
||||
import { config } from './config.js'
|
||||
import { EventType } from './eventTypes.js'
|
||||
import { resolve } from 'path'
|
||||
|
||||
function fileFilter(file) {
|
||||
if (config.get("allowedFiletypes").some(extension => file.path.endsWith(extension)))
|
||||
return true;
|
||||
if (file.stats.isDirectory())
|
||||
return true;
|
||||
return false;
|
||||
function fileFilter (file) {
|
||||
if (config.get('allowedFiletypes').some(extension => file.path.endsWith(extension))) { return true }
|
||||
if (file.stats.isDirectory()) { return true }
|
||||
return false
|
||||
}
|
||||
|
||||
export async function setupWatch(signaller) {
|
||||
const watch = new CheapWatch({
|
||||
dir: config.get("scriptsFolder"),
|
||||
filter: fileFilter,
|
||||
watch: !config.get("dry")
|
||||
});
|
||||
export async function setupWatch (signaller) {
|
||||
const watch = new CheapWatch({
|
||||
dir: config.get('scriptsFolder'),
|
||||
filter: fileFilter,
|
||||
watch: !config.get('dry')
|
||||
})
|
||||
|
||||
if (!config.get("quiet")) console.log("Watching folder", resolve(config.get("scriptsFolder")))
|
||||
if (!config.get('quiet')) console.log('Watching folder', resolve(config.get('scriptsFolder')))
|
||||
|
||||
watch.on('+', fileEvent => { if (fileEvent.stats.isFile()) signaller.emit(EventType.FileChanged, fileEvent) });
|
||||
watch.on('-', fileEvent => { if (fileEvent.stats.isFile()) signaller.emit(EventType.FileDeleted, fileEvent) });
|
||||
watch.on('+', fileEvent => { if (fileEvent.stats.isFile()) signaller.emit(EventType.FileChanged, fileEvent) })
|
||||
watch.on('-', fileEvent => { if (fileEvent.stats.isFile()) signaller.emit(EventType.FileDeleted, fileEvent) })
|
||||
|
||||
// Wait 'till filewatcher is ready to go
|
||||
await watch.init();
|
||||
// Wait 'till filewatcher is ready to go
|
||||
await watch.init()
|
||||
|
||||
if (config.get("dry")) {
|
||||
console.log("Watch would've synchronised:\n", watch.paths)
|
||||
process.exit();
|
||||
}
|
||||
if (config.get('dry')) {
|
||||
console.log("Watch would've synchronised:\n", watch.paths)
|
||||
process.exit()
|
||||
}
|
||||
|
||||
return watch;
|
||||
return watch
|
||||
}
|
94
src/index.js
94
src/index.js
|
@ -1,58 +1,58 @@
|
|||
"use strict"
|
||||
import { setupWatch } from "./fileWatch.js";
|
||||
import { config, loadConfig } from "./config.js";
|
||||
import { setupSocket } from "./networking/webSocket.js";
|
||||
import signal from "signal-js";
|
||||
import { fileChangeEventToMsg, fileRemovalEventToMsg, requestFilenames, requestDefinitionFile } from "./networking/messageGenerators.js";
|
||||
import { EventType } from "./eventTypes.js";
|
||||
import { messageHandler } from "./networking/messageHandler.js";
|
||||
'use strict'
|
||||
import { setupWatch } from './fileWatch.js'
|
||||
import { config, loadConfig } from './config.js'
|
||||
import { setupSocket } from './networking/webSocket.js'
|
||||
import signal from 'signal-js'
|
||||
import { fileChangeEventToMsg, fileRemovalEventToMsg, requestFilenames, requestDefinitionFile } from './networking/messageGenerators.js'
|
||||
import { EventType } from './eventTypes.js'
|
||||
import { messageHandler } from './networking/messageHandler.js'
|
||||
|
||||
export async function start() {
|
||||
loadConfig();
|
||||
const watch = await setupWatch(signal);
|
||||
const socket = setupSocket(signal);
|
||||
export async function start () {
|
||||
loadConfig()
|
||||
const watch = await setupWatch(signal)
|
||||
const socket = setupSocket(signal)
|
||||
|
||||
// Add a handler for received messages.
|
||||
signal.on(EventType.MessageReceived, msg => messageHandler(signal, msg, watch.paths));
|
||||
// Add a handler for received messages.
|
||||
signal.on(EventType.MessageReceived, msg => messageHandler(signal, msg, watch.paths))
|
||||
|
||||
// Add a handler for when a connection to a game is made.
|
||||
signal.on(EventType.ConnectionMade, () => {
|
||||
console.log("Connection made!");
|
||||
// Add a handler for when a connection to a game is made.
|
||||
signal.on(EventType.ConnectionMade, () => {
|
||||
console.log('Connection made!')
|
||||
|
||||
if (config.get("definitionFile").update) {
|
||||
signal.emit(EventType.MessageSend, requestDefinitionFile());
|
||||
}
|
||||
if (config.get('definitionFile').update) {
|
||||
signal.emit(EventType.MessageSend, requestDefinitionFile())
|
||||
}
|
||||
|
||||
if (config.get("pushAllOnConnection")) {
|
||||
const extensions = config.get("allowedFiletypes");
|
||||
for (const path of watch.paths.keys()) {
|
||||
if (extensions.some(extension => path.endsWith(extension)))
|
||||
signal.emit(EventType.MessageSend, fileChangeEventToMsg({ path: path }))
|
||||
}
|
||||
} else {
|
||||
// Upload missing files to the game.
|
||||
signal.emit(EventType.MessageSend, requestFilenames());
|
||||
}
|
||||
})
|
||||
if (config.get('pushAllOnConnection')) {
|
||||
const extensions = config.get('allowedFiletypes')
|
||||
for (const path of watch.paths.keys()) {
|
||||
if (extensions.some(extension => path.endsWith(extension))) { signal.emit(EventType.MessageSend, fileChangeEventToMsg({ path })) }
|
||||
}
|
||||
} else {
|
||||
// Upload missing files to the game.
|
||||
signal.emit(EventType.MessageSend, requestFilenames())
|
||||
}
|
||||
})
|
||||
|
||||
// Add a handler for changed files.
|
||||
signal.on(EventType.FileChanged, fileEvent => {
|
||||
if (!config.get("quiet")) console.log(fileEvent.path + " changed");
|
||||
signal.emit(EventType.MessageSend, fileChangeEventToMsg(fileEvent))
|
||||
});
|
||||
// Add a handler for changed files.
|
||||
signal.on(EventType.FileChanged, fileEvent => {
|
||||
if (!config.get('quiet')) console.log(fileEvent.path + ' changed')
|
||||
signal.emit(EventType.MessageSend, fileChangeEventToMsg(fileEvent))
|
||||
})
|
||||
|
||||
// Add a handler for removed files, if allowed.
|
||||
if (config.get("allowDeletingFiles"))
|
||||
signal.on(EventType.FileDeleted, fileEvent =>
|
||||
signal.emit(EventType.MessageSend, fileRemovalEventToMsg(fileEvent)));
|
||||
// Add a handler for removed files, if allowed.
|
||||
if (config.get('allowDeletingFiles')) {
|
||||
signal.on(EventType.FileDeleted, fileEvent =>
|
||||
signal.emit(EventType.MessageSend, fileRemovalEventToMsg(fileEvent)))
|
||||
}
|
||||
|
||||
console.log(`Server is ready, running on ${config.get("port")}!`)
|
||||
console.log(`Server is ready, running on ${config.get('port')}!`)
|
||||
|
||||
process.on('SIGINT', function () {
|
||||
console.log("Shutting down!");
|
||||
process.on('SIGINT', function () {
|
||||
console.log('Shutting down!')
|
||||
|
||||
watch.close();
|
||||
socket.close();
|
||||
process.exit();
|
||||
});
|
||||
watch.close()
|
||||
socket.close()
|
||||
process.exit()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,57 +1,54 @@
|
|||
import { readFileSync } from "fs";
|
||||
import { config } from "../config.js";
|
||||
import { join } from "path";
|
||||
import { readFileSync } from 'fs'
|
||||
import { config } from '../config.js'
|
||||
import { join } from 'path'
|
||||
|
||||
let messageCounter = 0;
|
||||
let messageCounter = 0
|
||||
|
||||
export function fileChangeEventToMsg({ path }) {
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "pushFile",
|
||||
"params": {
|
||||
"server": "home",
|
||||
"filename": addLeadingSlash(path),
|
||||
"content": readFileSync(join(config.get("scriptsFolder"), path)).toString()
|
||||
},
|
||||
"id": messageCounter++
|
||||
}
|
||||
export function fileChangeEventToMsg ({ path }) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
method: 'pushFile',
|
||||
params: {
|
||||
server: 'home',
|
||||
filename: addLeadingSlash(path),
|
||||
content: readFileSync(join(config.get('scriptsFolder'), path)).toString()
|
||||
},
|
||||
id: messageCounter++
|
||||
}
|
||||
}
|
||||
|
||||
export function fileRemovalEventToMsg({ path }) {
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "deleteFile",
|
||||
"params": {
|
||||
"server": "home",
|
||||
"filename": addLeadingSlash(path),
|
||||
},
|
||||
"id": messageCounter++
|
||||
}
|
||||
export function fileRemovalEventToMsg ({ path }) {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
method: 'deleteFile',
|
||||
params: {
|
||||
server: 'home',
|
||||
filename: addLeadingSlash(path)
|
||||
},
|
||||
id: messageCounter++
|
||||
}
|
||||
}
|
||||
|
||||
export function requestDefinitionFile() {
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "getDefinitionFile",
|
||||
"id": messageCounter++
|
||||
}
|
||||
export function requestDefinitionFile () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
method: 'getDefinitionFile',
|
||||
id: messageCounter++
|
||||
}
|
||||
}
|
||||
|
||||
export function requestFilenames() {
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "getFileNames",
|
||||
"params": {
|
||||
"server": "home",
|
||||
},
|
||||
"id": messageCounter++
|
||||
}
|
||||
export function requestFilenames () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
method: 'getFileNames',
|
||||
params: {
|
||||
server: 'home'
|
||||
},
|
||||
id: messageCounter++
|
||||
}
|
||||
}
|
||||
|
||||
function addLeadingSlash(path){
|
||||
const slashes = path.match('/');
|
||||
if (slashes)
|
||||
return `/${path}`
|
||||
else
|
||||
return path
|
||||
function addLeadingSlash (path) {
|
||||
const slashes = path.match('/')
|
||||
if (slashes) { return `/${path}` } else { return path }
|
||||
}
|
||||
|
|
|
@ -1,41 +1,39 @@
|
|||
import { messageTracker } from "./messageTracker.js";
|
||||
import { writeFile } from "fs";
|
||||
import { config } from "../config.js";
|
||||
import { EventType } from "../eventTypes.js";
|
||||
import { fileChangeEventToMsg } from "./messageGenerators.js";
|
||||
import { messageTracker } from './messageTracker.js'
|
||||
import { writeFile } from 'fs'
|
||||
import { config } from '../config.js'
|
||||
import { EventType } from '../eventTypes.js'
|
||||
import { fileChangeEventToMsg } from './messageGenerators.js'
|
||||
|
||||
export function messageHandler(signaller, msg, paths) {
|
||||
let incoming;
|
||||
export function messageHandler (signaller, msg, paths) {
|
||||
let incoming
|
||||
|
||||
try { incoming = JSON.parse(msg.toString()); }
|
||||
catch (err) { return console.log(err); }
|
||||
console.log(incoming)
|
||||
if (incoming.id == undefined) return;
|
||||
try { incoming = JSON.parse(msg.toString()) } catch (err) { return console.log(err) }
|
||||
console.log(incoming)
|
||||
if (incoming.id === undefined) return
|
||||
|
||||
if (incoming.result) {
|
||||
const request = messageTracker.get(incoming.id);
|
||||
if (request?.method &&
|
||||
request.method == "getDefinitionFile"
|
||||
&& incoming.result) {
|
||||
writeFile(config.get("definitionFile").location, incoming.result, (err) => {
|
||||
if (err) return console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (request?.method &&
|
||||
request.method == "getFileNames"
|
||||
&& incoming.result) {
|
||||
const gameFiles = incoming.result.map(file => removeLeadingSlash(file));
|
||||
|
||||
paths.forEach((stats, fileName) => {
|
||||
if (!stats.isDirectory() && !gameFiles.includes(fileName))
|
||||
signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName }));
|
||||
})
|
||||
}
|
||||
if (incoming.result) {
|
||||
const request = messageTracker.get(incoming.id)
|
||||
if (request?.method &&
|
||||
request.method === 'getDefinitionFile' &&
|
||||
incoming.result) {
|
||||
writeFile(config.get('definitionFile').location, incoming.result, (err) => {
|
||||
if (err) return console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
if (request?.method &&
|
||||
request.method === 'getFileNames' &&
|
||||
incoming.result) {
|
||||
const gameFiles = incoming.result.map(file => removeLeadingSlash(file))
|
||||
|
||||
paths.forEach((stats, fileName) => {
|
||||
if (!stats.isDirectory() && !gameFiles.includes(fileName)) { signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName })) }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeLeadingSlash(path) {
|
||||
const reg = /^\//;
|
||||
return path.replace(reg, "")
|
||||
function removeLeadingSlash (path) {
|
||||
const reg = /^\//
|
||||
return path.replace(reg, '')
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
class MessageTracker {
|
||||
data = new Map()
|
||||
#maxLength = 200
|
||||
data = new Map()
|
||||
#maxLength = 200
|
||||
|
||||
push(msg) {
|
||||
this.data.set(msg.id, msg);
|
||||
push (msg) {
|
||||
this.data.set(msg.id, msg)
|
||||
|
||||
if (this.data.size > this.#maxLength) {
|
||||
const [firstKey] = this.data.keys();
|
||||
this.data.delete(firstKey);
|
||||
}
|
||||
if (this.data.size > this.#maxLength) {
|
||||
const [firstKey] = this.data.keys()
|
||||
this.data.delete(firstKey)
|
||||
}
|
||||
}
|
||||
|
||||
get(index) {
|
||||
return this.data.get(index);
|
||||
}
|
||||
get (index) {
|
||||
return this.data.get(index)
|
||||
}
|
||||
}
|
||||
|
||||
export const messageTracker = new MessageTracker();
|
||||
export const messageTracker = new MessageTracker()
|
||||
|
|
|
@ -1,30 +1,28 @@
|
|||
import { WebSocketServer } from 'ws';
|
||||
import { config } from "../config.js";
|
||||
import { EventType } from "../eventTypes.js"
|
||||
import { requestDefinitionFile } from './messageGenerators.js';
|
||||
import { messageTracker } from "./messageTracker.js"
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { config } from '../config.js'
|
||||
import { EventType } from '../eventTypes.js'
|
||||
// import { requestDefinitionFile } from './messageGenerators.js'
|
||||
import { messageTracker } from './messageTracker.js'
|
||||
|
||||
export function setupSocket(signaller) {
|
||||
export function setupSocket (signaller) {
|
||||
const wss = new WebSocketServer({ port: config.get('port') })
|
||||
|
||||
const wss = new WebSocketServer({ port: config.get("port") });
|
||||
wss.on('connection', function connection (ws) {
|
||||
function sendMessage (msg) {
|
||||
messageTracker.push(msg)
|
||||
ws.send(JSON.stringify(msg))
|
||||
}
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', (msg) => {
|
||||
signaller.emit(EventType.MessageReceived, msg)
|
||||
})
|
||||
|
||||
function sendMessage(msg) {
|
||||
messageTracker.push(msg);
|
||||
ws.send(JSON.stringify(msg));
|
||||
}
|
||||
signaller.on(EventType.MessageSend, msg => {
|
||||
sendMessage(msg)
|
||||
})
|
||||
|
||||
ws.on('message', (msg) => {
|
||||
signaller.emit(EventType.MessageReceived, msg);
|
||||
});
|
||||
signaller.trigger(EventType.ConnectionMade)
|
||||
})
|
||||
|
||||
signaller.on(EventType.MessageSend, msg => {
|
||||
sendMessage(msg);
|
||||
});
|
||||
|
||||
signaller.trigger(EventType.ConnectionMade);
|
||||
});
|
||||
|
||||
return wss;
|
||||
return wss
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue