Nodejs访问全局范围内的嵌套变量

Nodejs访问全局范围内的嵌套变量

本文介绍了Nodejs访问全局范围内的嵌套变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何根据我下面的 NodeJS 代码在全局范围内访问套接字

How would I access socket in the global scope based on my following NodeJS code

io.on('connection', function (socket) {
console.log('connection '+socket)
    socket.on("data",function(d){console.log('data from flash: ',d);});
    socket.emit("message","wtfwtwftwftwf hello from server");
    socket.on('disconnect', function (socket) {
        console.log("disconnect");
    });

});

我需要从以下 app.post 方法中访问套接字

I need to access socket from within the following app.post method

var express = require('express'),
multer  = require('multer');
var app = express();

//auto save file to uploads folder
app.use(multer({ dest: './uploads/'}))

app.post('/', function (req, res) {
console.log(req.body); //contains the variables
console.log(req.files); //contains the file references
res.send('Thank you for uploading!');
});

app.listen(8080);

app.listen(8080);

还没有测试,但要先尝试一个简单的getter函数

Haven't tested yet but going to try a simple getter function first

io.on('connection', function (socket) {
    console.log('connection '+socket)
        socket.on("data",function(d){console.log('data from flash: ',d);});
        socket.emit("message","wtfwtwftwftwf hello from server");
return{
getSocket: function(){
return socket;
}
};
        socket.on('disconnect', function (socket) {
            console.log("disconnect");
        });

});

io.getSocket() ??

推荐答案

Express 的 app 和 Socket.io 没有任何关系.

Express's app and Socket.io have nothing to do with one another.

所以从根本上说,你不能在 app.post 中使用 socket.

So fundamentally, you can't use socket inside app.post.

您需要确定客户.您可以使用具有 Passportrel="nofollow">Socket.io 插件 本质上将 app.post/getreq.user 桥接到socket.request.user.

You need to identify the client. You can use Passport which has a Socket.io plugin that essentially bridges the app.post/get's req.user to socket.request.user.

注意:它不必是具有从数据库中提取的用户的身份验证客户端,只需具有存储在内存中的临时用户的客户端即可.像这样:

Note: It doesn't have to be an authenticated client with user that's fetched from database, just a client with a temporary user stored in memory would do. Something like this:

app.use(function(req, res, next) {
    if (!req.user) { // if a user doesn't exist, create one
        var id = crypto.randomBytes(10).toString('hex');
        var user = { id: id };
        req.logIn(user);
        res.redirect(req.lastpage || '/');
        return;
    }
    next();
});

var Users = {};
passport.serialize(function(user) {
    Users[user.id] = user;
    done(null, user);
});
passport.deserialize(function(id) {
    var user = Users[id];
    done(null, user);
});

然后您可以将客户端的套接字 ID 附加到其用户会话中.

Then you can attach the client's socket ID to its user session.

io.on('connection', function (socket) {
    socket.request.user.socketid = socket.id
});

然后在 app.post 中使用 socketidsocket.emit 使用 io.emit/p>

And then instead of socket.emit use io.emit in app.post using the socketid

app.post('/', function (req, res) {
    io.to(req.user.socketid).emit('whatever');
});

注意:io.to() 用于发送到 room,但默认情况下每个 socket 都连接到与其 socket 命名的同一个房间.id,所以它会工作.

Note: io.to() is used to emit to a room, but every socket is by default joined to the same room named as its socket.id, so it'll work.

这篇关于Nodejs访问全局范围内的嵌套变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:03