-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (83 loc) · 2.79 KB
/
Copy pathapp.js
File metadata and controls
101 lines (83 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// MongoDB
const mongo = require("mongodb").MongoClient;
const dsn = "mongodb://localhost:27017/chatlog";
// Express
const express = require('express');
const app = express();
const port = 8000;
const bodyParser = require('body-parser');
const cors = require('cors');
app.use(cors());
app.use(bodyParser.json());
const server = require('http').createServer(app);
const io = require('socket.io')(server);
// Return a JSON object with list of all documents within the collection.
app.get("/chatlog", async (request, response) => {
try {
let res = await findInCollection(dsn, "messages", {}, {}, 0);
// console.log(res);
response.json(res);
} catch (err) {
console.log(err);
response.json(err);
}
});
/**
* Reset a collection by removing existing content and insert a default
* set of documents.
*
* @async
*
* @param {string} dsn DSN to connect to database.
* @param {string} colName Name of collection.
* @param {string} doc Documents to be inserted into collection.
*
* @throws Error when database operation fails.
*
* @return {Promise<void>} Void
*/
async function updateChatlog(dsn, colName, doc) {
const client = await mongo.connect(dsn, { useUnifiedTopology: true });
const db = await client.db();
const col = await db.collection(colName);
await col.insertOne(doc);
await client.close();
}
/**
* Find documents in an collection by matching search criteria.
*
* @async
*
* @param {string} dsn DSN to connect to database.
* @param {string} colName Name of collection.
* @param {object} criteria Search criteria.
* @param {object} projection What to project in results.
* @param {number} limit Limit the number of documents to retrieve.
*
* @throws Error when database operation fails.
*
* @return {Promise<array>} The resultset as an array.
*/
async function findInCollection(dsn, colName, criteria, projection, limit) {
const client = await mongo.connect(dsn, { useUnifiedTopology: true });
const db = await client.db();
const col = await db.collection(colName);
const res = await col.find(criteria, projection).limit(limit).toArray();
await client.close();
return res;
}
io.origins(['https://listrom.me:443','http://localhost:3000']);
io.on('connection', function (socket) {
console.info("User connected");
socket.on('chat message', function (message) {
const timestamp = new Date().toLocaleTimeString();
const stampedMessage = timestamp + " - " + message;
io.emit('chat message', stampedMessage);
updateChatlog(dsn, "messages", { message: stampedMessage })
.catch(err => console.log(err));
});
});
server.listen(port, () => {
console.log(`Socket server listening on port ${port}!`);
console.log(`DSN is: ${dsn}`);
});