如何通过socket.io更改cookie的值?

时间:2016-11-14 10:53:53

标签: javascript sockets express cookies socket.io

我需要回答这个问题作为标题。如何通过socket.io更改cookie的值?

以下是我的全部代码:

const path = require('path');
var cookieParser = require('cookie-parser')
var cookieIoParser = require('socket.io-cookie');

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

 app.use('/css', express.static(path.join(__dirname, 'css')))
 app.use('/js', express.static(path.join(__dirname, 'js')))

 app.use(cookieParser())
 io.use(cookieIoParser)

 /*
  * Access-Control-Allow to /fb-dialog
  */

 app.get('/fb-dialog', function (req, res, next) {

  var wildcard = true;
  var credentials = true;

  var Origin = (wildcard && !credentials || !req.headers.origin ? '*' : req.headers.origin);

    res.setHeader('Access-Control-Allow-Origin', Origin);
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH');
    res.setHeader('Access-Control-Allow-Headers', 'Origin,X-Requested-With,content-type');

    if(credentials){
    res.setHeader('Access-Control-Allow-Credentials', true);
    }

    next();

 })


 app.get('/fb-dialog', function(req, res){

   var cookies = cookieParser.JSONCookies(req.cookies);

   if(!cookies.fbdialog){
   res.cookie('fbdialog', 'true')
   }

  if(req.headers.origin){

    res.send('<html>...</html>'); // Here is creating a html

  }

 });


 /*
  * Socket.io
  */

 io.on('connection', function(socket){

 console.log('a user connected: ' + socket.id);

   socket.on('card-heading', function(){

   var cookies = socket.request.headers.cookie;

   if(cookies.fbdialog){
     socket.emit('cookie', 'fbdialog=false');
     console.log('false: ' + cookies.fbdialog);
   }else{
     socket.emit('cookie', 'fbdialog=true');
     console.log('true: ' + cookies.fbdialog);
   }
   });

   socket.on('typing', function(){
   socket.broadcast.emit('typing');
   });

   socket.on('clear', function(){
   socket.broadcast.emit('clear');
   });

   socket.on('chat message', function(msg){
   console.log('the user wrote:' +msg);
   socket.broadcast.emit('chat message', msg);
   });

 });

 http.listen(3000, function(){
   console.log('Example app listening on port 3000!')
})

这是我的整个代码。我在if块中使用了你的例子,但正如我所说,我总是得到socket.emit(&#39; cookie&#39;,&#39; fbdialog = true&#39;);

2 个答案:

答案 0 :(得分:0)

一个简单的解决方案(更新):

cookie.js:

'use strict';
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const cookieIoParser = require('socket.io-cookie-parser');
var cookies;

app.get('/', function(req, res){
    res.sendFile(__dirname+'/cookie.html');
});

io.use(cookieIoParser());

io.on('connect', function(socket){
  cookies = socket.request.cookies;
  console.log(cookies);
  if(cookies.fbdialog){
    socket.emit('cookie', 'fbdialog=false');
  } else {
    socket.emit('cookie', 'fbdialog=true');
  }
});

http.listen(3001, function(){
    console.log('listening on *:3001');
});

cookie.html

<!DOCTYPE html>
<html>
  <head>
    <title>Sending cookies with socket.io</title>
  </head>
  <body>
    <h1 id='socket'>not connected</h1>
    <h3 id='username'></h3>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io();       
      socket.on('connect', function(){
        document.getElementById('socket').innerHTML = 'connected to the server';
      });      
      socket.on('cookie', function(cookie) {
        document.cookie = cookie;
        document.getElementById('username').innerHTML = 'cookies: ' + document.cookie;
      });
      socket.on('disconnected',function(){
        document.getElementById('socket').innerHTML = 'disconnected';
      });        
    </script>
  </body>  
</html>
  1. 运行npm install socket.io-cookie-parser --save
  2. 使用node cookie启动服务器,然后使用浏览器转到localhost:3001
  3. 请记住,设置Cookie的整个操作都发生在cookie.html客户端socket.on('cookie'...)代码中。

答案 1 :(得分:0)

我修改了服务器:

'use strict';
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const cookie = require('cookie');
var cookies;

app.get('/', function(req, res){
    res.sendFile(__dirname+'/cookie.html');
});

io.on('connect', function(socket){
  cookies = cookie.parse(socket.request.headers.cookie);
  if(cookies.fbdialog){
    socket.emit('cookie', 'fbdialog=false');
  } else {
    socket.emit('cookie', 'fbdialog=true');  
  }
});

http.listen(3001, function(){
    console.log('listening on *:3001');
});

我希望现在可以按照您的意愿使用。