-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.55 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.use(allowCrossDomain);
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(cookieParser());
var port = process.env.PORT || 3000;
io.on('connection', function(socket) {
console.log("connected")
socket.on('chat message', function(msg) {
console.log("chat message", msg)
io.emit('chat message', msg);
});
socket.on('error', (error) => {
console.log("chat message", "error")
});
socket.on('disconnect', (reason) => {
console.log("chat message", "disconnect")
});
});
app.post('/signin',function(req, res, next){
console.log(req.body);
res.cookie("userLogin" , req.body).send('Cookie is set');
console.log(req.cookies);
});
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
});
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
});
});
http.listen(port, function() {
console.log('listening on *:' + port);
});