我如何在节点/快捷路由和Socket.io之间进行通信?

时间:2021-12-20 03:35:37

I would like to use socket.io to keep track of daily active users of an application. My socket connection looks like this:

我想用插座。io跟踪应用程序的日常活动用户。我的套接字连接是这样的:

let visitorData = {};

io.on('connection', (socket) => {
    socket.on('user', (data) => {
        visitorData[socket.id] = data;
    })

    socket.on('disconnect', () => {
        delete visitorData[socket.id]
    })
})

I would like to be able to incorporate existing Node/Express routes and the data received to socket connections. In particular, how would I handle a POST to a /login endpoint to associate the login username to the socket id connection.

我希望能够合并现有的节点/高速路由和接收到套接字连接的数据。特别是,我如何处理到/login端点的POST,以将登录用户名与套接字id连接相关联。

For example:

例如:

app.get('/login', (req, res, next) => {
  const someSocket = /* socket */
  const someSocketId = /* socket id */;

  visitorData[someSocketId] = req.body.username;

  someSocket.emit('user', visitorData);
})

Is there a way I can incorporate socket.io into Node/Express routes to (a) associate the user with a socket.id and (b) emit information from a request/response body?

有没有办法我可以合并插座。将io连接到节点/高速路由(a),将用户与套接字关联起来。id和(b)从请求/响应主体发出信息?

1 个解决方案

#1


2  

I set up something just like this using passport socket.io. It creates middleware that allows you to access users from sockets. Here is some of my implementation:

我是用护照和护照来设置的。它创建了一个中间件,允许您从socket访问用户。以下是我的一些实现:

app.js:

app.js:

import GameSocket from './lib/game-socket';

var app = express();

GameSocket(app, 3700);

lib/game-socket.js:

lib / game-socket.js:

import Server from 'socket.io';
import passportSocketIo from 'passport.socketio';
import cookieParser from 'cookie-parser';

// Configured redisStore for authentication
import {redisStore} from '../lib/session';

// Configured passport strategy for authentication
import passport from '../lib/passport';

export default function GameSocket(app, port) {
  // Setup socket
  var server = new Server();
  var io = server.listen(app.listen(port));

  // Setup authentication
  io.use(passportSocketIo.authorize({
    key: 'connect.sid',
    secret: 'your_secret',
    store: redisStore,
    passport: passport,
    cookieParser: cookieParser,
    success: (data, accept) => accept(null, true),
    failure: (data, message, err, accept) => {
      console.log(err);
      if(err) throw new Error(message);
    }
  }));

  // On connection listener
  io.sockets.on('connection', function (socket) {

      // This is an example of getting user info from the socket
      console.log('Success: New connection with: ', socket.request.user.username);

      // Add your socket events here
    });

  return io;
}

In order to use this you need to use a session store. I personally used a redis session store, connect-redis. This is configured in lib/session.js.

为了使用它,您需要使用会话存储。我个人使用了一个redis会话存储,connect-redis。这是在lib/session.js中配置的。

#1


2  

I set up something just like this using passport socket.io. It creates middleware that allows you to access users from sockets. Here is some of my implementation:

我是用护照和护照来设置的。它创建了一个中间件,允许您从socket访问用户。以下是我的一些实现:

app.js:

app.js:

import GameSocket from './lib/game-socket';

var app = express();

GameSocket(app, 3700);

lib/game-socket.js:

lib / game-socket.js:

import Server from 'socket.io';
import passportSocketIo from 'passport.socketio';
import cookieParser from 'cookie-parser';

// Configured redisStore for authentication
import {redisStore} from '../lib/session';

// Configured passport strategy for authentication
import passport from '../lib/passport';

export default function GameSocket(app, port) {
  // Setup socket
  var server = new Server();
  var io = server.listen(app.listen(port));

  // Setup authentication
  io.use(passportSocketIo.authorize({
    key: 'connect.sid',
    secret: 'your_secret',
    store: redisStore,
    passport: passport,
    cookieParser: cookieParser,
    success: (data, accept) => accept(null, true),
    failure: (data, message, err, accept) => {
      console.log(err);
      if(err) throw new Error(message);
    }
  }));

  // On connection listener
  io.sockets.on('connection', function (socket) {

      // This is an example of getting user info from the socket
      console.log('Success: New connection with: ', socket.request.user.username);

      // Add your socket events here
    });

  return io;
}

In order to use this you need to use a session store. I personally used a redis session store, connect-redis. This is configured in lib/session.js.

为了使用它,您需要使用会话存储。我个人使用了一个redis会话存储,connect-redis。这是在lib/session.js中配置的。