【问题标题】:How can I add multiple chat rooms in RTCMultiConnection V3.0 in a single pageRTCMultiConnection V3.0如何在一个页面中添加多个聊天室
【发布时间】:2017-09-05 06:32:12
【问题描述】:

我想在一个页面上创建多个聊天室,用户可以在其中为不同的房间进行视频/音频/文本聊天。我已经完成了一个,但我对单个页面上的连接没有太多想法。

我在单人聊天室使用RTCMultiConnection V3.0

我想实现下图中提到的。

谁能帮我实现这个目标?

【问题讨论】:

    标签: webrtc rtcmulticonnection


    【解决方案1】:

    要么使用多个 RTCMultiConnection 实例:

    var room1 = new RTCMultiConnection();
    var room2 = new RTCMultiConnection();
    var room3 = new RTCMultiConnection();
    
    room1.join('room1-unique-ID');
    room2.join('room2-unique-ID');
    room3.join('room3-unique-ID');
    

    或者使用这个技巧:

    connection.session.broadcast = true;
    connection.open('each-user-unique-id'); // each user should call this
    

    现在每个用户都可以加入任何房间:

    connection.join('first-room');
    connection.join('second-room');
    connection.join('third-room');
    

    记住,我们在上面设置了{broadcast:true};这意味着join 方法只允许您加入房间所有者/主持人/发起者。

    你需要让房间管理员给你他所有参与者的名单。

    var alreadyGivedParticipants = {};
    
    connection.onPeerStateChanged = function(state) {
        if (state.iceConnectionState !== 'connected') return; // ignore
        if (alreadyGivedParticipants[state.userid]) return;
        alreadyGivedParticipants[state.userid] = true; // to make sure we don't duplicate
    
        connection.socket.emit(connection.socketCustonEvent, {
            participants: connection.getAllParticipants(),
            onlyForThisUser: state.userid
        });
    };
    
    connection.connectSocket(function() {
        connection.socket.on(connection.socketCustonEvent, function(message) {
            if (message.onlyForThisUser !== connection.userid) return; // ignore
    
            message.participants.forEach(function(pid) {
                connection.join(pid); // join all room participants
            });
        });
    });
    

    您甚至可以将每个新用户的信息提供给现有参与者。

    var alreadyGivedParticipants = {};
    
    connection.onPeerStateChanged = function(state) {
        if (state.iceConnectionState !== 'connected') return; // ignore
        if (alreadyGivedParticipants[state.userid]) return;
        alreadyGivedParticipants[state.userid] = true; // to make sure we don't duplicate  
    
        connection.socket.emit(connection.socketCustonEvent, {
            participants: connection.getAllParticipants(),
            onlyForThisUser: state.userid
        });
    
        // below block shares new user with existing participants
        connection.getAllParticipants().forEach(function(existingUser) {
            connection.socket.emit(connection.socketCustonEvent, {
                participants: [state.userid],
                onlyForThisUser: existingUser
            });
        });
    };
    
    connection.connectSocket(function() {
        connection.socket.on(connection.socketCustonEvent, function(message) {
            if (message.onlyForThisUser !== connection.userid) return; // ignore
    
            message.participants.forEach(function(pid) {
                connection.join(pid); // join all room participants
            });
        });
    });
    

    您可以使用getPublicModerators 方法获取所有公共房间的列表。一个live demo

    【讨论】:

    • 是的。以上所有示例均适用于 v3。
    【解决方案2】:

    使用 PHP 创建房间列表比使用 rtcmulti...publicModarator 更容易,只需创建一个带有 id、opener、joiner、stat、timejoin 的表(房间)。 使用Ajax代码在表中写入initiator'name并在主文件本身中访问它,为joiners创建一个单独的文件,它只会显示那些已经openef/streamed的人。

    对于多房间/类别,您需要一个变量“类型”,然后使用具有搜索机制的精确 mysql !!!

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2016-07-06
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多