-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (41 loc) · 1.49 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express')
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
let ent = require('ent');
let session = require('express-session')({
secret: 'pseudo',
resave: true,
saveUninitialized: true
});
let sharedsession = require("express-socket.io-session");
app.use(session)
.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js'))
.use('/js', express.static(__dirname + '/node_modules/jquery/dist'))
.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css'))
.use('/css', express.static(__dirname + '/public/css/'))
.get('/', function (req, res) {
res.sendFile('view/index.html' , { root : __dirname});
});
io.use(sharedsession(session));
io.sockets.on('connection', function (socket, pseudo) {
socket.emit('connect');
socket.on('newbie', function(pseudo) {
pseudo=ent.encode(pseudo);
socket.handshake.session.pseudo = pseudo;
socket.handshake.session.save();
socket.broadcast.emit('newbie', socket.handshake.session.pseudo)
})
.on('msg', function(msg) {
msg=ent.encode(msg);
pseudo=socket.handshake.session.pseudo
socket.broadcast.emit('msg', {pseudo : pseudo, msg: msg })
})
.on('disconnect', function(){
let pseudo=socket.handshake.session.pseudo
socket.broadcast.emit("diconnected", {pseudo: pseudo});
})
});
server.listen(8080, () => {
console.info('server up on 8080')
});