简体   繁体   中英

In my code i am trying to create rooms but i don't know why every time it create old rooms with new one

This my server.js file. In this i am creating a new room on every create room request and then push that created room into the rooms array. Username and roomname is taken form the form by post request.But i don't know why it is creating all the previous rooms every time with new socket id like. If i give it a username:abc and roomname:room1 first time it will create a room then next time if i would create a room username:abc2 and roomname:room2 with new socket connection at that time it would again create the old room(username:abc,roomname:room1) first and increase the rooms array length and then create the new room(username:abc2,roomname:room2) and increase the length of rooms array.

//server.js


const express = require('express');
const app = new express();
const socket = require('socket.io');
const bodyParser = require('body-parser');
const port = process.env.PORT || 8000;
const server = app.listen(port,() =>{
    console.log(`server is running on the port number ${port}`);
})


const io = socket(server);


app.use(express.static(`${__dirname}/public`));
app.set('view engine','ejs');
app.set('views',`${__dirname}/views`);
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
let rooms = [];

function pushRoom(rooms,usernme,roomname){
let room = {
    name:roomname,
    users:[]
}
 room.users.push(usernme);
 rooms.push(room);
} 

function joinRoom(rooms,roomjoinname,username){
for(let i=0 ; i<rooms.length ; i++){
    if(rooms[i].name == roomjoinname){
        rooms[i].users.push(username);
    }
}
}

app.get('/',(req,res,next) =>{
 res.render('index');
})

//taking values from user by post request

    app.post('/chat',(req,res,next) =>{
        io.on('connection',socket =>{
        console.log('made a socket connection'+socket.id);
        let roomName = req.body.roomName;
        let userName = req.body.username;
        let roomNameJoin = req.body.roomNameJoin;
            pushRoom(rooms,userName,roomName);
            socket.join('roomName');
});
res.render('chat');
});

Reason for repeated additions

This is because every time a user visits the /chat page you add a new listener to your socket.io server. So if 3 users have visited the /chat page, 3 event listeners will have been registered for the connection event. This means that when a new connection will be made at that point, the 3 registered listeners will be executed and three new rooms will be pushed to your array.

Suggestion that removes unwanted behaviour

Put your room creation outside the socket.io connection event handler and put your connection handler registration outside of your HTTP post handler.

app.post('/chat', (req,res,next) => {
  let roomName = req.body.roomName;
  let userName = req.body.username;
  pushRoom(rooms,userName,roomName);
  res.render('chat');
});

io.on('connection', socket => {
  console.log('made a socket connection'+socket.id);
  socket.join('roomName');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM