Created
April 3, 2015 17:50
-
-
Save httpNick/28e3f50d8f8c6b4b87f6 to your computer and use it in GitHub Desktop.
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 3/20/15. | |
*/ | |
var express = require('express'); | |
var app = express(); | |
var port = 8888; | |
var dbURL = "localhost:27017/test_reddit_database"; | |
var collections = ["user_test"]; | |
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.user_test.save({username: data.username, comments: data.comments}); | |
}); | |
socket.on('isInHere', function(data) { | |
db.user_test.find({username: data}, function(err, docs) { | |
console.log(docs.length); | |
if (docs.length > 0) { | |
socket.emit('isInHereResponse', true); | |
} else { | |
socket.emit('isInHereResponse', false); | |
} | |
}) | |
}); | |
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