Added session disconnect handling

This commit is contained in:
Mitchell McCaffrey 2020-03-16 20:34:32 +11:00
parent 65e102292b
commit 8010c1fb4a
2 changed files with 13 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState, useRef } from "react";
import React, { useState, useRef } from "react";
import {
ThemeProvider,

View File

@ -20,11 +20,22 @@ function useSession(imgRef) {
...prevConnnections,
[connection.peer]: connection
}));
connection.on("open", () => {
if (imgRef.current) {
connection.send(imgRef.current);
}
});
function removeConnection() {
setConnections(prevConnections => {
const { [connection.peer]: old, ...rest } = prevConnections;
return rest;
});
}
connection.on("close", removeConnection);
connection.on("error", removeConnection);
}
if (!peer) {
@ -37,7 +48,7 @@ function useSession(imgRef) {
peer.removeListener("open", handleOpen);
peer.removeListener("connection", handleConnection);
};
}, [peer, peerId, connections]);
}, [peer, peerId, connections, imgRef]);
return [peer, peerId, connections];
}