Commit 8681721c authored by Home's avatar Home
Browse files

maj

parent f1dada23
......@@ -59,7 +59,7 @@ app.get('/', function(req,res)
{
return res.redirect('/signin');
}
res.render('home', { title: 'Home'});
res.render('home', { title: 'Home',user:req.session.user});
});
......
/*const express= require('express');
//const express= require('express');
const swig = require('swig');
const session = require('express-session')
const parser =require('body-parser');
const users = require('./user');
const messages = require('./messages');*/
const messages = require('./messages');
var app = require('express')();
const swig = require('swig');
var server = require('http').Server(app);
var io = require('socket.io')(server);
......@@ -13,20 +12,6 @@ app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
server.listen(8008);
app.get('/', function (req, res) {
res.render('home');
});
io.on('connection', function (socket) {
io.sockets.on('connection', function (socket) {
socket.emit('message', 'Vous êtes bien connecté !');
});
});
/*
app.use(session({
secret: 'keyboard'
}));
......@@ -38,9 +23,44 @@ app.use(function(req,res,next)
console.log('method -> ' + req.method);
console.log('url -> ' + req.url);
next();
});
});
let connected={};
io.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = utils.parseCookie(data.headers.cookie);
data.sessionID = data.cookie['connect.sid'].split('.')[0].substring(2);
sessionStore.get(data.sessionID, function (err, session) {
// now you have all session variables
});
}
});
io.sockets.on('connection', function (socket) {
let user;
var address = socket.handshake.address;
console.log('New connection from ' + address.address + ':' + address.port);
socket.on('conencted',function(e){
});
socket.emit('message', 'Vous êtes bien connecté !');
socket.on('init',function(c){
connected[c.user]={socket,page,page:c.page};
console.log(connected.toString())
/*user=c.user;
users[socket.id] = [session, socket];*/
});
socket.on('disconnect', function(){
console.log('Suppression de '+user);
delete connected[user];
})
});
app.get('/signin',function(req,res){
if(req.session.user)
{
......@@ -74,7 +94,7 @@ app.get('/', function(req,res)
{
return res.redirect('/signin');
}
res.render('home', { title: 'Home'});
res.render('home', { title: 'Home',user:req.session.user});
});
......@@ -133,5 +153,14 @@ app.use(function(req,res,next){
res.status(404).render('404',{ title : '404',user : req.session.user});
})
app.listen(8008);
server.listen(8008);
/*
*/
......@@ -4,10 +4,16 @@
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
<!--<script>
var socket = io.connect('http://localhost:8008');
socket.on('message', function(message) {
alert('Le serveur a un message pour vous : ' + message);
})
</script>
socket.on('message', function(message) {
var div = document.getElementById("toAdd");
var article = document.createElement("article");
article.innerHTML = message;
div.appendChild(article);
});
window.onbeforeunload = function (e) {
socket.emit('ciao','je me deconnecte');
};
</script>-->
{% endblock %}
{%extends 'layout.html'%}
{% block content %}
<h1>Messages Publiques</h1>
<div id="messages">
{% for message in messages %}
<article>
<p> Auteur : {{ message.author }} </p>
<p> Message : {{ message.text }} </p>
</article>
{% endfor %}
</div>
<h3>Ajouter un message</h3>
<form action="" method="post">
<p><label for="text">Veuillez entrer votre message :</label>
<input type="textarea" name="text" placeholder="Veuillez entrer votre message"></p>
<input type="submit" value="valider">
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8008');
console.log(user);
socket.emit('init',{page:'public',{%user%})});
</script>
{% endblock %}
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