Created
November 12, 2014 05:04
-
-
Save unoseistres/40e9a7cfa2cbce25fdbd to your computer and use it in GitHub Desktop.
server.js
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
/* | |
//////////////////////////////// HTTP Portion | |
// HTTP Portion | |
var http = require('http'); | |
var fs = require('fs'); // Using the filesystem module | |
var httpServer = http.createServer(requestHandler); | |
var url = require('url'); | |
httpServer.listen(8088); | |
function requestHandler(req, res) { | |
var pathname = req.url; | |
if (pathname == '/') { // if blank let's ask for index.html | |
pathname = '/mobile.html'; | |
} | |
var parsedUrl = url.parse(req.url); | |
console.log("The Request is: " + parsedUrl.pathname); | |
// Read index.html | |
fs.readFile(__dirname + parsedUrl.pathname, | |
// Callback function for reading | |
function (err, data) { | |
// if there is an error | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading ' + parsedUrl.pathname); | |
} | |
// Otherwise, send the data, the contents of the file | |
res.writeHead(200); | |
res.end(data); | |
} | |
); | |
/* | |
res.writeHead(200); | |
res.end("Life is wonderful"); | |
} | |
*/ | |
// HTTP Portion | |
var http = require('http'); | |
var fs = require('fs'); // Using the filesystem module | |
var httpServer = http.createServer(requestHandler); | |
var url = require('url'); | |
httpServer.listen(8088); | |
function requestHandler(req, res) { | |
var parsedUrl = url.parse(req.url); | |
//console.log("The Request is: " + parsedUrl.pathname); | |
// Read index.html | |
fs.readFile(__dirname + parsedUrl.pathname, | |
// Callback function for reading | |
function (err, data) { | |
// if there is an error | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading ' + parsedUrl.pathname); | |
} | |
// Otherwise, send the data, the contents of the file | |
res.writeHead(200); | |
res.end(data); | |
} | |
); | |
} | |
// WebSocket Portion | |
// WebSockets work with the HTTP server | |
var io = require('socket.io').listen(httpServer); | |
var idarray=[]; | |
var useridarray=[]; | |
for (var i = 0; i < 6; i++) { | |
idarray[i] = null; | |
} | |
// Register a callback function to run when we have an individual connection | |
// This is run for each individual user that connects | |
io.sockets.on('connection', | |
// We are given a websocket object in our function | |
function (socket) { | |
console.log("We have a new client: " + socket.id); | |
if (idarray.indexOf(socket.id) != -1){} | |
else{ | |
//idarray.push(socket.id) | |
for (var i = 0; i < 6; i++) { | |
if (idarray[i] == null) { | |
idarray[i] = socket.id; | |
break; | |
} | |
} | |
} | |
socket.on('image', function(data) { | |
console.log(socket.id+"send"); | |
// Data comes in as whatever was sent, including objects | |
var newvideo={}; | |
newvideo.clientid=socket.id; | |
newvideo.array=idarray; | |
// newvideo.userid=randomid; | |
io.sockets.emit('imageback',newvideo); | |
//user version | |
// var usernewvideo={}; | |
// usernewvideo.clientid=userid; | |
// usernewvideo.array=useridarray; | |
// io.sockets.emit('imageback',usernewvideo); | |
// console.log(userid); | |
}); | |
// cid++; | |
// When this user emits, client side: socket.emit('otherevent',some data); | |
socket.on('chatmessage', function(data) { | |
// Data comes in as whatever was sent, including objects | |
console.log("Received: 'chatmessage' " + data); | |
// Send it to all of the clients | |
io.emit('chatmessage', data); | |
}); | |
socket.on('backcolor', function(data) { | |
// Data comes in as whatever was sent, including objects | |
console.log("Received: 'backcolor' " + data); | |
// Send it to all of the clients | |
io.emit('backcolor', data); | |
/* socket.broadcast.emit('backcolor', data); */ | |
}); | |
/* | |
socket.on('image', function(data) { | |
console.log(socket.id+"send"); | |
// Data comes in as whatever was sent, including objects | |
var newvideo={}; | |
newvideo.clientid=socket.id; | |
newvideo.array=idarray; | |
// newvideo.userid=randomid; | |
io.sockets.emit('imageback',newvideo); | |
}); | |
*/ | |
socket.on('sharon', function(sharondata) { | |
console.log("Received: 'sharon' " + sharondata); | |
io.sockets.emit('sharon', sharondata); | |
}); | |
socket.on('disconnect', function() { | |
console.log("Client has disconnected"); | |
var index = idarray.indexOf(socket.id); | |
//idarray.splice(index, 1); | |
idarray[index] = null; | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment