Added client side validation for join party and fix session error handling not removing conenction

This commit is contained in:
Mitchell McCaffrey 2020-07-20 20:54:21 +10:00
parent 8542f4d4cc
commit 81897faef0
2 changed files with 20 additions and 4 deletions

View File

@ -85,14 +85,17 @@ class Connection extends SimplePeer {
// and to use our custom data handler
createDataChannel(channelName, channelConfig, opts) {
const channel = super.createDataChannel(channelName, channelConfig, opts);
this.dataChannels[channelName] = channel;
channel.on("data", this.handleData.bind(this));
this.handleDataChannel(channel);
return channel;
}
handleDataChannel(channel) {
this.dataChannels[channel.channelName] = channel;
const channelName = channel.channelName;
this.dataChannels[channelName] = channel;
channel.on("data", this.handleData.bind(this));
channel.on("error", (error) => {
this.emit("error", error);
});
}
// Converted from https://github.com/peers/peerjs/

View File

@ -3,6 +3,8 @@ import { EventEmitter } from "events";
import Connection from "./Connection";
import { omit } from "./shared";
/**
* @typedef {object} SessionPeer
* @property {string} id - The socket id of the peer
@ -93,6 +95,16 @@ class Session extends EventEmitter {
* @param {string} password - the password of the party
*/
async joinParty(partyId, password) {
if (typeof partyId !== "string" || typeof password !== "string") {
console.error(
"Unable to join party: invalid party ID or password",
partyId,
password
);
this.emit("disconnected");
return;
}
this._partyId = partyId;
this._password = password;
try {
@ -159,8 +171,9 @@ class Session extends EventEmitter {
}
function handleClose() {
peer.connection.destroy();
this.emit("disconnect", { peer });
peer.connection.destroy();
this.peers = omit(this.peers, [peer.id]);
}
function handleError(error) {