-
-
Save naeluh/892885ca3e62592da091 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var express = require('express'), | |
multer = require('multer'), | |
app = express(), | |
http = require('http'), | |
server = http.createServer(app), | |
io = require('socket.io').listen(server); | |
server.listen(6969); | |
app.use(express.static(__dirname + '/static')); | |
app.get('/', function (req, res) { | |
res.render('index.ejs'); | |
}); | |
app.get('/test', function (req, res) { | |
res.render('test.ejs'); | |
}); | |
var usernames = {}; | |
var rooms = {}; | |
var roomFileArr = {}; | |
var userColor = {}; | |
var value = isNaN(value) ? 0 : value; | |
var imgName; | |
app.get('/:room', function (req, res, next) { | |
if (rooms[req.params.room]) { | |
console.log('Sent'); | |
res.render('room.ejs'); | |
} else { | |
console.log('Redirected'); | |
res.redirect('/'); | |
} | |
}); | |
app.get('/file/:room', function (req, res, next) { | |
if (roomFileArr[req.params.room]) { | |
console.log(roomFileArr[req.params.room]); | |
var fileToSend = roomFileArr[req.params.room]; | |
res.send(fileToSend); | |
} else { | |
console.log('NotFound'); | |
res.send(404); | |
} | |
}); | |
app.post('/api', multer({ | |
dest: './static/files/', | |
rename: function (fieldname, filename) { | |
return filename + Date.now(); | |
}, | |
onFileUploadStart: function (file) { | |
console.log(file.originalname + ' is starting ...') | |
}, | |
onFileUploadComplete: function (file) { | |
console.log(file.fieldname + ' uploaded to ' + file.path); | |
imgName = file.name; | |
}, | |
onError: function (err, next) { | |
console.log(err); | |
next(err); | |
} | |
}), function (req, res) { | |
console.log(req.files || {}); | |
console.log('Image Path ' + imgName || {}); | |
res.send(imgName); | |
return res.send(200); | |
}); | |
io.sockets.on('connection', function (socket) { | |
socket.on('adduser', function (username, color) { | |
if(username === undefined || username === null || username === ''){ | |
username = 'fake user #' + value++; | |
socket.username = username; | |
usernames[username] = username; | |
userColor[username] = color; | |
}else{ | |
socket.username = username; | |
usernames[username] = username; | |
userColor[username] = color; | |
} | |
}); | |
socket.on('addroom', function (room) { | |
socket.join(room); | |
console.log('Room ' + room || {}); | |
socket.emit('updatechat', 'SERVER', '#fff', 'you have connected to ' + room); | |
socket.rooms = room; | |
rooms[room] = room; | |
console.log('new user ' + socket.username || {}); | |
io.sockets. in (socket.room).emit('updatechat', 'SERVER', '#fff' ,socket.username +' has joined this room'); | |
socket.emit('updaterooms', rooms, room); | |
}); | |
socket.on('update_coords', function(pos) { | |
var x, _ref; | |
var mouseColor = '#fff'; | |
try { | |
//for (x = 0, _ref = usernames.length; 0 <= _ref ? x <= _ref : x >= _ref; 0 <= _ref ? x++ : x--) { | |
if (userColor[socket.username]) { | |
var mouseColor = userColor[socket.username]; | |
console.log("Client: " + socket.id); | |
console.log("X: " + pos.x + ", Y: " + pos.y); | |
} | |
//} | |
} catch (err) { | |
console.log(err); | |
} | |
return io.sockets.emit("send_data", pos , socket.username , mouseColor); | |
}); | |
socket.on('sendchat', function (data) { | |
console.log('new user ' + socket.username || {}); | |
if (userColor[socket.username]) { | |
var colors = userColor[socket.username]; | |
io.sockets. in (socket.room).emit('updatechat', socket.username, colors, data); | |
} else { | |
var colors = '#fff'; | |
io.sockets. in (socket.room).emit('updatechat', socket.username, colors, data); | |
} | |
}); | |
socket.on('switchRoom', function (newroom) { | |
socket.leave(socket.room); | |
socket.join(newroom); | |
console.log('newroom ' + newroom || {}); | |
socket.emit('updatechat', 'SERVER', '#fff', 'you have connected to ' + newroom); | |
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', '#fff', socket.username + ' has left this room'); | |
socket.room = newroom; | |
rooms[newroom] = newroom; | |
io.sockets. in (socket.room).emit('updatechat', 'SERVER', '#fff' ,socket.username +' has joined this room'); | |
socket.emit('updaterooms', rooms, newroom); | |
}); | |
socket.on('disconnect', function () { | |
socket.broadcast.emit('updatechat', 'SERVER', '#fff', socket.username + ' has disconnected'); | |
delete usernames[socket.username]; | |
io.sockets.emit('updateusers', usernames); | |
socket.leave(socket.room); | |
}); | |
socket.on('draw', function (dataControls) { | |
io.sockets. in (socket.room).emit('updateControls', socket.username, socket.room, dataControls); | |
}); | |
socket.on('updateFile', function (newFile, userRoom) { | |
console.log(newFile || {}); | |
console.log(userRoom || {}); | |
console.log(roomFileArr || {}); | |
io.sockets. in (socket.room).emit('addFile', socket.username, userRoom, newFile); | |
roomFileArr[userRoom] = imgName; | |
console.log(roomFileArr || {}); | |
}); | |
socket.on('session', function (whichSession) { | |
io.sockets. in (socket.room).emit('checkSession', whichSession); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment