forked from julianbrowne/rtsdemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sswatcher.js
54 lines (38 loc) · 1.2 KB
/
sswatcher.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
/**
*
* RTS Demo - Single Mongo Server Watcher
*
**/
var http = require('http');
var socket = require('socket.io');
var mongo = require('mongodb');
require('./lib/cursorextension.js').extendCursor(mongo);
var utils = require('./lib/utils.js');
// mongo/demo settings
var mongoHost = 'localhost';
var mongoPort = 8001;
var dbname = 'rtsdemo';
var collname = 'statstore';
var wsPort = 8011;
// start socket server
var socketsApp = http.createServer();
var io = socket.listen(socketsApp);
io.set('log level', 1);
socketsApp.listen(wsPort);
console.log("SS Watcher WS server running on ws://localhost:" + wsPort);
// create mongo connection and make a db
var mongoServer = new mongo.Server(mongoHost, mongoPort, {});
var dbConnection = new mongo.Db(dbname, mongoServer, {});
// Open db and wait for browser to connect
dbConnection.open(function(error,db){
if(error)
{
console.error("Error: Open database failed. Is mongod running?");
process.exit(1);
}
var coll = db.collection(collname);
io.sockets.on('connection', function (socket) {
console.log('Received browser connection.');
utils.readAndSend(socket, coll, 'stats-channel');
});
});