forked from rauchg/chat-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit rauchg#2: Make changes to index.js
- Loading branch information
1 parent
27c748a
commit 2da5c85
Showing
1 changed file
with
72 additions
and
13 deletions.
There are no files selected for viewing
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
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); | ||
}); | ||
}); |