Created
April 5, 2015 05:09
-
-
Save httpNick/b89fe67c1f5a7acf4f3c to your computer and use it in GitHub Desktop.
updated server code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by httpnick on 4/4/15. | |
*/ | |
/** | |
* Created by httpnick on 3/20/15. | |
*/ | |
var express = require('express'); | |
var app = express(); | |
var port = 8888; | |
var dbURL = "localhost:27017/initialdb"; | |
var collections = ["initialdb"]; | |
var db = require("mongojs").connect(dbURL, collections); | |
var io = require("socket.io").listen(app.listen(port)); | |
console.log("Server Started."); | |
var connections = 0; | |
io.sockets.on("connection", function (socket) | |
{ | |
connections++; | |
console.log("New connection. We are now at " + connections + " connections."); | |
socket.on('insert', function (data) { | |
console.log(data.comments); | |
db.initialdb.save({username: data.username, | |
comments: data.comments, | |
sub: data.sub, | |
vote: data.vote, | |
flair: data.flair, | |
time:data.time}); | |
}); | |
socket.on('isInHere', function(data) { | |
db.initialdb.find({username: data}, function(err, docs) { | |
console.log(data + "exists this many times: " + docs.length); | |
if (docs.length > 0) { | |
socket.emit('isInHereResponse', true); | |
} else { | |
socket.emit('isInHereResponse', false); | |
} | |
}) | |
}); | |
socket.on('getDBStats', function() { | |
db.initialdb.stats(function(data) { | |
console.log(data); | |
socket.emit('getDBStatsResponse', data); | |
}); | |
}); | |
socket.on('connectionCount', function() { | |
socket.emit('connectionCountResponse', connections) | |
}); | |
socket.on('test', function (data) { | |
console.log(data); | |
}); | |
socket.on('disconnect', function() { | |
console.log("A disconnect happened. " + (--connections) + " are now active"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment