-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·49 lines (39 loc) · 1.29 KB
/
index.js
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
"use strict";
const http = require('http');
const mongoose = require('mongoose');
const api = require('./src/api');
const config = require('./src/config');
const sockjs = require('sockjs');
// Set the port to the API.
api.set('port', config.port);
//Create a http server based on Express
const server = http.createServer(api);
//Connect to the MongoDB database; then start the server
mongoose
.connect(config.mongoURI)
.then(() => server.listen(config.port))
.catch(err => {
console.log('Error connecting to the database', err.message);
process.exit(err.statusCode);
});
//Websocket server setup
const echo = sockjs.createServer({ sockjs_url: config.sockjsURL });
let clients=[];
echo.on('connection', function(conn) {
clients.push(conn);
conn.on('data', function(message) {
for (var i=0; i < clients.length; i++)
clients[i].write(message);
});
conn.on('close', function() {
clients = clients.filter(client => client.id !== conn.id);
});
});
echo.installHandlers(server, {prefix:'/echo'});
server.on('listening', () => {
console.log(`API is running in port ${config.port}`);
});
server.on('error', (err) => {
console.log('Error in the server', err.message);
process.exit(err.statusCode);
});