Skip to content

Commit

Permalink
Commit rauchg#2: Make changes to index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSachinSBhat committed Feb 7, 2017
1 parent 27c748a commit 2da5c85
Showing 1 changed file with 72 additions and 13 deletions.
85 changes: 72 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
'use strict';

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io');

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
var server = http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;

http.listen(3000, function(){
console.log('listening on *:3000');
switch (path) {
case '/':
path = "/index.html";
break;
default:
response.writeHead(404);
response.write('File not found!');
response.end();
break;
}
});

// Use this while debugging in node-debug
//server.listen(8001);

// Use this while deploying to heroku
server.listen(process.env.PORT);

var listener = io.listen(server);

//global.testContext = 0;
/* Array that holds the location data of all the devices currently connected */
global.deviceLocationData = [];
/* Array that holds the session data for all the connected clients */
global.allClients = [];

listener.sockets.on('connection', function (socket) {
/* Push the current socket to the allClients array */
allClients.push(socket);
var deviceSessionId = allClients.indexOf(socket);

socket.emit('message', { 'message': 'hello world'});

socket.emit('deviceSessionData', { 'deviceSessionId': deviceSessionId });

socket.on('disconnect', function () {
console.log('Got disconnect!');

var deviceSessionId = allClients.indexOf(socket);
/* Remove the session from the allClients array */
allClients.splice(deviceSessionId, 1);

/* Remove the location data from the deviceLocationData array */
deviceLocationData.splice(deviceSessionId, 1);

// TODO: Convert this into a method - broadcastBusLocationData(socket)
socket.broadcast.emit('buslocationData', {
'deviceLocationData': global.deviceLocationData
});
});

socket.on('buslocation', function (busLocation) {

global.deviceLocationData[busLocation.deviceSessionId] = { 'deviceSessionId': busLocation.deviceSessionId, 'lat': busLocation.lat, 'lng': busLocation.lng };

// TODO: Convert this into a method - broadcastBusLocationData(socket)
socket.broadcast.emit('buslocationData', {
//'deviceSessionId': busLocation.deviceSessionId, 'lat': busLocation.lat, 'lng': busLocation.lng,
'deviceLocationData': global.deviceLocationData
});
});

socket.on('chat message', function(msg){
socket.emit('chat message', msg);
});
});

0 comments on commit 2da5c85

Please sign in to comment.