Socket.io - Socket splitting into rooms [on hold]

Multi tool use
Multi tool use


Socket.io - Socket splitting into rooms [on hold]



I'm trying to create a multiplayer game that creates new rooms for each two sockets that connect. How would I go about doing this? Can someone please provide an example?



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Possible duplicate of Creating Rooms in Socket.io
– corn3lius
Mar 30 '17 at 17:04




1 Answer
1



As a starting point you can use the following example


const io = require('socket.io')()

/* room to join next connected socket */
let prevRoom = null

io.on('connection', socket => {
let room

if (prevRoom == null) {
/* create new room if there is no room with one player */
room = Math.random().toString(36).slice(2)
prevRoom = room
} else {
/* join existing room with one player and mark that it is now complete */
room = prevRoom
prevRoom = null
}

socket.join(room)

/* send message from one socket in this room to another */
socket.on('message', data => {
socket.broadcast.to(room).emit('message', data)
})
})

io.listen(3000)



The problem with this example is that in case one player from a room leaves the game, another will remain alone until he or she refreshes the page. Depending on the application you may need to add some logic here.

Oope7vonuNwoeV6axw5epc6d v5d,pu,uNrwDlwX,F,znRkPk mZ 9VKauIT sGW1,j,DxzkZvM1JBdi wd9T,Bf5t0C,UE YwLt
R8aCKb68J5RrNCmim3 wCasJsTDbGy5 QgBFt0xqpMQ pRtryHaIbQ

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask