【问题标题】:Make a cookie when a user connects with socket. io - node js当用户使用套接字连接时制作一个 cookie。 io - 节点 js
【发布时间】:2018-01-14 05:25:24
【问题描述】:

有一个使用 socket.io 的服务器。当用户连接时,它将为他们分配在服务器上创建的用户 ID,然后将其增加 1,以便下一个具有不同 ID 的用户。

我想为此使用 cookie,以检查他们是否以前登录过,如果是,则使用该 id,如果没有,则使用服务器上的那个。

创建cookie的方法是使用

res.cookie('cookie', 'monster')

但我没有放在哪里,我试着把它放在连接函数中,但 res 不存在。如果我把它放在函数之外,我该怎么称呼它?这是我的代码。这是我的服务器的开始:

//Require npm modules
var express = require('express'); 
var http = require('http');
var events = require('events');
var io = require('socket.io');
var ejs = require('ejs');
var app = express();

//Set the default user Id to 1 and the default username to Guest
exports.Server = Server = function()
{
    this.userId = 1;
    this.userName = "Guest";
};

app.set('view engine', 'ejs');
app.get('/game/:id', function (req, res)
{
    res.render('game', {game: req.params.id});
});


Server.prototype.initialise = function(port)
{
    //Create the server using the express module
    this.server = http.createServer(app);

    //Declare the 'public' folder and its contents public
    app.use(express.static('public'));  

    //Listen to any incoming connections on the declared port and start using websockets
    this.server.listen(port);
    this.startSockets();
    this.em = new events();

    consoleLog('SERVER', 'Running on port: ' + port);
};

Server.prototype.startSockets = function()
{
    //When a user connects to the server on the 'game' socket
    this.socket = io.listen(this.server);

    this.socket.of('game').on('connection', function(user)
        {
            res.cookie('cookie', 'monster')
            //Set their usedId and username
            user.userId = this.userId;
            user.userName = this.userName + " " + this.userId;

            //Increment the user id by 1 so each user with get a unique id
            this.userId++;

            //Send a response back to the client with the assigned username and user id and initialise them
            user.emit('connected', user.userId, user.userName);
            this.em.emit('initialiseUser', user.userId, user.userName);

所以我有 res.cookie 的地方就是我希望能够读取和写入 cookie 的地方,任何帮助都会得到帮助

【问题讨论】:

  • 您引用了res,但它未定义。它可能会引发错误。
  • 是的,这就是我的全部问题
  • 需要传入res作为参数。您一定缺少一些代码,因为我在任何地方都没有看到 initialise() 被调用。
  • 我从另一个 js 文件初始化它... var server = new Server(); server.initialise(8081);

标签: javascript node.js sockets cookies


【解决方案1】:

我认为您正在寻找的是 express 采用的中间件模式。您可以根据需要定义任意数量的这些中间件调用,它们是调用任何其他可能需要 res 实例(或 req 实例那件事)。

app.use(function (req, res, next) {
  // call function, passing in res here
  next();
})

参考:https://expressjs.com/en/guide/using-middleware.html

编辑:

此答案不适合您的情况。在不使用套接字连接的节点/快速服务器中,是的,您可以在需要范围内的请求和响应对象的任何地方轻松使用上述模式。

但是,一旦您设置了 socket io 服务器,游戏就会改变。在套接字通信期间,范围内不再有明确的请求和响应对象,一切都在您的套接字处理代码和客户端之间直接处理。所以答案是你需要用socket io的方式来处理这种情况,而不是用express的方式。

请看:Adding a cookie value on Socket.IO

【讨论】:

  • 我试过这个,当我出于某种原因连接时,它总共运行了 7 次。我将如何从这里设置用户 ID?
  • 啊,好吧,所以您需要考虑将套接字 io 连接与请求和响应的快速路由分开。建立套接字连接后,将其视为与套接字处理代码的直接连接,这意味着不会有 res 或 req 实例,但此时您不需要它们。当你发出一个套接字时,你可以发回你想要的任何数据。然后,您在另一端接收数据的代码可以决定在客户端看到特定类别的发射时在客户端上设置一个 cookie,例如您的代码示例中的“已连接”。
  • 好的,所以您是说客户端加载后应该检查客户端的cookie id 并将该id 发送到服务器中的连接函数。然后,如果 id 在服务器上的 id 数组中,则使用该用户 id 登录。我认为这可能会奏效,试一试
  • 可能有很多方法可以实现您的目标。请查看我编辑的答案。
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 2015-09-12
  • 2021-10-08
  • 2021-09-14
  • 1970-01-01
  • 2021-12-24
  • 2021-11-02
  • 2017-02-04
相关资源
最近更新 更多