first commit with ugly code
This commit is contained in:
parent
4f8306077e
commit
7a3fbbb8c1
79
indes.js
Normal file
79
indes.js
Normal file
@ -0,0 +1,79 @@
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { MongoClient } from 'mongodb';
|
||||
|
||||
const mongo = new MongoClient(process.env.MONGO_CONN_STR);
|
||||
const db = mongo.db();
|
||||
const collections = {};
|
||||
|
||||
const wss = new WebSocketServer({ port: 7130 });
|
||||
const connections = [];
|
||||
const subscriptions = [];
|
||||
|
||||
async function executeSet(message, ws) {
|
||||
let subject = collections[message.subject];
|
||||
if (!subject) {
|
||||
subject = collections[message.subject] = db.collection(message.subject);
|
||||
}
|
||||
let newValue = {$set: {}};
|
||||
newValue.$set[message.field + '.modifiedAt'] = Date.now();
|
||||
newValue.$set[message.field + '.modifiedBy'] = 'anonymous';
|
||||
newValue.$set[message.field + '.transientValue'] = message.value;
|
||||
|
||||
await subject.updateMany(message.filter, newValue, { upsert: true })
|
||||
}
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
connections.push(ws);
|
||||
console.log('accepted new connection');
|
||||
|
||||
ws.on('error', console.error);
|
||||
ws.on('close', e => {
|
||||
console.log('closed a connection');
|
||||
connections.splice(connections.indexOf(ws), 1);
|
||||
});
|
||||
|
||||
ws.on('message', function message(data, isBinary) {
|
||||
console.log('received a new message');
|
||||
|
||||
try {
|
||||
let message = JSON.parse(data);
|
||||
console.log('message action is ' + message.action);
|
||||
|
||||
switch (message.action) {
|
||||
case 'set':
|
||||
executeSet(message, ws).catch(console.error);
|
||||
break;
|
||||
case 'subscribe':
|
||||
if (!subscriptions[message.subject]) {
|
||||
subscriptions[message.subject] = {};
|
||||
}
|
||||
if (!subscriptions[message.subject][JSON.stringify(message.filter)]) {
|
||||
subscriptions[message.subject][JSON.stringify(message.filter)] = {};
|
||||
}
|
||||
if (!subscriptions[message.subject][JSON.stringify(message.filter)][message.field]) {
|
||||
subscriptions[message.subject][JSON.stringify(message.filter)][message.field] = [];
|
||||
}
|
||||
subscriptions[message.subject][JSON.stringify(message.filter)][message.field].push(ws);
|
||||
let subject = collections[message.subject];
|
||||
if (!subject) {
|
||||
subject = collections[message.subject] = db.collection(message.subject);
|
||||
}
|
||||
subject.find(message.filter, {}).toArray().then(r => {
|
||||
ws.send(JSON.stringify({
|
||||
subject: message.subject,
|
||||
field: message.field,
|
||||
filter: message.filter,
|
||||
value: r.map(v => v[message.field])
|
||||
}));
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error('error while processing message', e);
|
||||
ws.send(JSON.stringify(e));
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
17
package.json
Normal file
17
package.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node --env-file=.env index.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"mongodb": "^6.13.0",
|
||||
"ws": "^8.18.0"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user