问题
I'm creating a web game using socket.io and express. I want to let users create their lobby and I want to use socket.io to emit data (position, etc...) only to the lobby that they're connected. How can I do it? For the moment I found this code online but it emit data to all the lobby that are created.
index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', {
title: 'Create lobby',
errors: req.session.errors
});
req.session.errors = null;
});
router.get('/lobby/:id', function(req, res, next){
res.render('test', {output: req.params.id} );
})
router.post('/lobby/submit', function(req, res, next){
var id = req.body.id;
res.redirect('/lobby/' + id);
})
module.exports = router;
index.html
<form action="/lobby/submit" method="post">
<input type="lobby" name="id">
<button type="submit">Submit</button>
</form>
来源:https://stackoverflow.com/questions/58982887/game-lobby-with-socket-io-and-express