Set password to protect your room
connection.password = 'abcdef';
connection.open('room-id', function(isRoomOpened, roomid, error) {
if(isRoomOpened === true) {
alert('Room opened with password: ' + connection.password);
}
});
Please try this demo: demos/Password-Protected-Rooms.html
// HTML Elements:
// <input type="text" id="room-id">
// <button id="open-room">open-room</button>
// <button id="join-room">join-room</button>
// <input type="checkbox" id="protect-room-with-password" checked>
document.getElementById('open-room').onclick = function() {
if (document.getElementById('protect-room-with-password').checked === true) {
var password = prompt('Please Enter Password');
if (!password || !password.length) {
alert('Please enter a valid password.');
return;
}
connection.password = password; // <------check this line
}
connection.open(document.getElementById('room-id').value, function(isRoomOpened, roomid, error) {
if (error) {
if (error === 'Room not available') {
alert('Someone already created this room. Please either join or create a separate room.');
return;
}
alert(error);
}
});
};
document.getElementById('join-room').onclick = function() {
if (document.getElementById('protect-room-with-password').checked === true) {
var password = prompt('Please Enter Password');
if (!password || !password.length) {
alert('Please enter a valid password.');
return;
}
connection.password = password;
}
connection.join(document.getElementById('room-id').value, function(isJoinedRoom, roomid, error) {
if (error) {
if (error === 'Invalid password') {
var password = prompt('Please Enter Password');
if (!password || !password.length) {
alert(error);
return;
}
connection.password = password;
document.getElementById('join-room').onclick();
return;
}
if (error === 'Room not available') {
alert('This room does not exist. Please either create it or wait for moderator to enter in the room.');
return;
}
alert(error);
}
});
};