Commit 4e45ce09 authored by Corentin Mors's avatar Corentin Mors
Browse files

Enhance server with more comments

parent e3d5c024
// Setup basic express server
var express = require('express'),
app = express(),
path = require('path'),
server = require('http').createServer(app),
io = require('socket.io')(server),
port = process.env.PORT || 3000,
port = process.env.PORT || 1500,
bodyParser = require('body-parser');
// Start listening
server.listen(port, () => {
console.log('Server listening at port %d', port);
});
......@@ -19,12 +19,12 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Define templating with Nunjucks engine
var nunjucks = require('nunjucks');
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.set('view engine', 'html');
......@@ -34,7 +34,7 @@ app.use(require('./routes'))
// GeoChat Room
var numinroom = [];
var numinroom = []; // Store number of users in each room
io.on('connection', (socket) => {
var addedUser = false;
......@@ -43,8 +43,9 @@ io.on('connection', (socket) => {
socket.on('add user', (username, room) => {
if (addedUser) return;
console.log("Received add user :" + username + " in room " + room);
// console.log("Received add user : " + username + " in room " + room);
// Insert user in the chosen room
socket.join(room);
socket.room = room;
......@@ -55,13 +56,13 @@ io.on('connection', (socket) => {
numinroom[room] = 1;
}
// we store the username in the socket session for this client
// Store the username in the socket session for this client
socket.username = username;
addedUser = true;
socket.emit('login', {
numUsers: numinroom[room]
});
// echo globally (all clients) that a person has connected
// Echo globally (all clients in the room) that a person has connected
socket.in(room).broadcast.emit('user joined', {
username: socket.username,
numUsers: numinroom[room]
......@@ -69,7 +70,7 @@ io.on('connection', (socket) => {
});
// when the client emits 'new message', this listens and executes
// When a client emits 'new message', this listens and executes
socket.on('new message', (data) => {
// we tell the client to execute 'new message'
socket.in(socket.room).broadcast.emit('new message', {
......@@ -79,8 +80,8 @@ io.on('connection', (socket) => {
});
// When a client emits 'geoloc' data, this listen and executes
socket.on('geoloc', (data) => {
// we tell the client to execute 'new message'
//console.log("Got data " + data + " from " + socket.username);
socket.in(socket.room).broadcast.emit('geoloc', {
username: socket.username,
......@@ -88,21 +89,21 @@ io.on('connection', (socket) => {
});
});
// when the client emits 'typing', we broadcast it to others
// When a client emits 'typing', we broadcast it to others in room
socket.on('typing', () => {
socket.in(socket.room).broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
// When a client emits 'stop typing', we broadcast it to others in room
socket.on('stop typing', () => {
socket.in(socket.room).broadcast.emit('stop typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
// When the user disconnects.. we delete him and broadcast in the room
socket.on('disconnect', () => {
if (addedUser) {
if (numinroom[socket.room] == 1){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment