我在服务器端有一个socket.js文件,并且希望将我的用户通信分成不同的名称空间,即
this.mainRoom = io.of('/main_room')
this.privateRoom = io.of('/private_room')
当用户加入特定于'/ main_room'名称空间的房间时,它可以正常工作。 但我希望用户能够加入“ / private_room”命名空间。 这是代码
const socks = function (io) {
let self = this
self.privateRoom = null
// where all the messaging and the events that are not part of the
// user-to-user communication
this.mainRoom = io.of('/main_room')
this.mainRoom.on('connection', (socket) => {
socket.on('set_name', (data) => {
const nickname = data.name
socket.nickname = nickname
socket.emit('name_set', data)
socket.send(JSON.stringify({
type: 'serverMessage',
nickname: socket.nickname,
message: "Welcome! "
})
)
socket.broadcast.emit('user_entered', {
nickname: socket.nickname,
})
})
socket.on('join_room', (room) => {
// concept roomname !== namespace
// concept Rooms are subchannels of the namespaces.
//create another socket, since socketio doesnt allow a single socket
//to be connected to multiple namespaces
socket.join(room.name, () => {
---------->> how do i allow user to join the room in the private_room namespace? <<---------
---------->> how should i create socket2 to join the same room.name? <<----------
// let socket2 = self.privateRoom.sockets[socket.id]
// socket2.join(room.name)
})
})
})
// split the socket code into 2 separate parts for namespaces
// this is so that the messaging and events that are related to users
// are in a namespace
// while messaging and events that are not part of user-to-user
// communication is separated
this.privateRoom = io.of('/private_room')
this.privateRoom.on('connection', (socket) => {
socket.on('message', (message) => {
message = JSON.parse(message)
if (message.type === "userMessage") {
socket.in(socket.room).broadcast.send(JSON.stringify(message))
message.type = "myMessage"
socket.send(JSON.stringify(message))
}
})
})
}
答案 0 :(得分:0)
只需将this.privateRoom的套接字重命名为socket2并使其成为全局变量
self.socket2 = null
this.privateRoom = io.of('/private_room')
this.privateRoom.on('connection', (socket2) => {
self.socket2 = socket2
self.socket2.on('message', (message) => {
message = JSON.parse(message)
if (message.type === "userMessage") {
self.socket2.in(self.socket2.room).send(JSON.stringify(message))
message.type = "myMessage"
self.socket2.send(JSON.stringify(message))
}
})
})
并将其返回给mainRoom
let socket2 = self.socket2.join(room.name)
socket2.room = room.name
socket.in(room.name).emit('user_entered', {'name': socket.nickname})