1

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

我将RTCMultiConnection V3.0用于单个聊天室。

我想实现下图中提到的。

多个聊天室

谁能帮我实现这一目标?

4

2 回答 2

2

使用多个 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方法获取所有公共房间的列表。现场演示

于 2017-09-05T11:22:59.337 回答
0

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

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

于 2018-03-31T16:59:43.337 回答