Added session disconnect handling
This commit is contained in:
parent
65e102292b
commit
8010c1fb4a
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useState, useRef } from "react";
|
import React, { useState, useRef } from "react";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ThemeProvider,
|
ThemeProvider,
|
||||||
|
@ -20,11 +20,22 @@ function useSession(imgRef) {
|
|||||||
...prevConnnections,
|
...prevConnnections,
|
||||||
[connection.peer]: connection
|
[connection.peer]: connection
|
||||||
}));
|
}));
|
||||||
|
|
||||||
connection.on("open", () => {
|
connection.on("open", () => {
|
||||||
if (imgRef.current) {
|
if (imgRef.current) {
|
||||||
connection.send(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) {
|
if (!peer) {
|
||||||
@ -37,7 +48,7 @@ function useSession(imgRef) {
|
|||||||
peer.removeListener("open", handleOpen);
|
peer.removeListener("open", handleOpen);
|
||||||
peer.removeListener("connection", handleConnection);
|
peer.removeListener("connection", handleConnection);
|
||||||
};
|
};
|
||||||
}, [peer, peerId, connections]);
|
}, [peer, peerId, connections, imgRef]);
|
||||||
|
|
||||||
return [peer, peerId, connections];
|
return [peer, peerId, connections];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user