-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (50 loc) · 1.47 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const fs = require('fs');
const env = process.env.NODE_ENV || 'dev';
const config = require('./config')[env];
const app = express();
// Bootstrap db connection
var promise = mongoose.connect(config.db, {
useMongoClient: true
});
mongoose.Promise = global.Promise;
// Bootstrap models
const models_path = __dirname + '/model'
fs.readdirSync(models_path).forEach(function (file) {
if (~file.indexOf('.js')) require(models_path + '/' + file)
});
app.use(bodyParser.urlencoded({ extended: true }));
require('./routes')(app);
app.use(express.static(path.join(__dirname, 'public')));
app.listen(config.app.hport, function(){
console.log('HTTP Server Listening on port %d', config.app.hport);
console.log('WS Server Listening on port %d', config.app.wsport);
});
/**
* WebSocket Server
**/
const WebSocket = require('ws').Server;
var port = config.app.wsport;
var ws = new WebSocket({port: port});
ws.on('connection', function(w){
w.on('message', function(data){
ws.clients.forEach(function each(client) {
if (client !== w) {
client.send(data);
}
});
console.log('message from client :', data);
});
w.on('close', function() {
console.log('closing connection');
});
});
/**
* Helper Functions
**/
function randInt(){
return Math.floor(Math.random() * arr.length);
}