From 2da5c85ae801ad5a35d744a7d9664db259cbfcaa Mon Sep 17 00:00:00 2001 From: Sachin Bhat Date: Wed, 8 Feb 2017 02:26:18 +0530 Subject: [PATCH] Commit #2: Make changes to index.js --- index.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index a322dca0..1e1d35c5 100644 --- a/index.js +++ b/index.js @@ -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); + }); +}); \ No newline at end of file