Skip to content

Instantly share code, notes, and snippets.

@racingcow
Created Jun 25, 2012
Embed
What would you like to do?
youRhere-part3-index.js
var app = require("express").createServer();
var io = require("socket.io").listen(app);
var tp = require("./targetprocess");
app.listen(8080);
//routing
app.get("/", function(req, res) {
res.sendfile(__dirname + "/index.html");
});
app.get("/index.css", function(req, res) {
res.sendfile(__dirname + "/index.css");
});
//users currently connected
var _usernames = {};
//Global TP options
tp.api({
username: "myusername",
password: "mypassword",
url: "https://mycompanyname.tpondemand.com/api/v1/"
});
io.sockets.on("connection", function(socket){
//get completed user stories and bugs for the current iteraion
tp.api("getEntitiesForActiveIteration", function(entities) {
//console.log("returned from TP call");
socket.emit("entitiesretrieved", entities);
});
//when the client emits "adduser", this listens and executes
socket.on("adduser", function(username) {
//store username in the socket session for this client, and in global list
socket.username = username;
_usernames[username] = username;
//tell the client that he or she has been connected
socket.emit("updateaudit", "SERVER", "you have connected");
//tell everyone else that he or she has been connected
socket.broadcast.emit("updateaudit", "SERVER", username + " has connected");
//update the list of users on the client side
io.sockets.emit("updateusers", _usernames);
});
//user changes active item
socket.on("changeactiveitem", function(activeItemId, activeItemName) {
socket.emit("updateaudit", "SERVER", "you changed the active item to '" + activeItemName + "'");
socket.broadcast.emit("updateaudit", "SERVER", socket.username + " changed the active item to '" + activeItemName + "'");
io.sockets.emit("activeitemchanged", activeItemId);
});
//handle client disconnects
socket.on("disconnect", function() {
//remove the username from the global list
delete _usernames[socket.username];
//update the list of users on the client side
io.sockets.emit("updateusers", _usernames);
//tell everyone that the user left
socket.broadcast.emit("updateaudit", "SERVER", socket.username + " has disconnected.");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment