Game lobby with socket.io and express

天大地大妈咪最大 提交于 2019-12-12 06:50:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!