Socket-IO如何使用套接字ID加入会议室

时间:2018-10-16 09:08:33

标签: node.js

  1. 如何在一个房间中使用其套接字ID添加一个或多个成员?我试过使用socket [socketId] .join(roomName),它不起作用。

1 个答案:

答案 0 :(得分:-1)

服务器端

var server = http.Server(app);
var io = require('socket.io')(server);
io.on('connection', function (socket){
    console.log('Connected');
    socket.on('addMe', function(s) {
        console.log('Joining Room');
        console.log(s);
        socket.join(s);
        console.log('Joined to Room');
    });
    socket.on('disconnect', function () {
       console.log('Disconnected');
    });
 });

客户端

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.dev.js"></script>

var socket = io.connect('/');
var roomName = "blah";
socket.emit('addMe', roomName);