-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor network module * make it null before destroy, fix #20
- Loading branch information
Showing
3 changed files
with
110 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,62 @@ | ||
import Peer from "peerjs"; | ||
|
||
export type Conn = Peer["connections"][0]; | ||
export const isValidPeerJsId = ( | ||
roomId: string, | ||
peerJsId: unknown | ||
): peerJsId is string => | ||
typeof peerJsId === "string" && peerJsId.startsWith(`${roomId}_`); | ||
|
||
export const isConnectedConn = (conn: Conn, ignoreConnecting = false) => { | ||
if (!conn) return false; | ||
const peerConn = conn.peerConnection; | ||
if (!peerConn) return false; | ||
const connState = peerConn.connectionState; | ||
if (connState === "connected") return true; | ||
if (!ignoreConnecting) { | ||
if (connState === "connecting" || connState === "new") { | ||
return true; | ||
} | ||
} | ||
// for safari | ||
if (!connState && peerConn.signalingState === "stable") return true; | ||
return false; | ||
}; | ||
export const generatePeerJsId = (roomId: string, peerId: number) => | ||
`${roomId}_${peerId}`; | ||
|
||
export const getPeerIdFromPeerJsId = (peerJsId: string) => | ||
Number(peerJsId.split("_")[1]); | ||
|
||
export const getPeerIdFromConn = (conn: Peer.DataConnection) => | ||
getPeerIdFromPeerJsId(conn.peer); | ||
|
||
export const getLivePeers = (myPeer: Peer) => { | ||
const peers = Object.keys(myPeer.connections); | ||
const livePeers = peers.filter((peer) => | ||
myPeer.connections[peer].some((conn: Conn) => isConnectedConn(conn, true)) | ||
); | ||
return livePeers; | ||
export const createConnectionMap = () => { | ||
type Value = { | ||
conn: Peer.DataConnection; | ||
live: boolean; | ||
}; | ||
const map = new Map<string, Value>(); | ||
const addConn = (conn: Peer.DataConnection) => { | ||
map.set(conn.peer, { conn, live: false }); | ||
}; | ||
const markLive = (conn: Peer.DataConnection) => { | ||
const value = map.get(conn.peer); | ||
if (value) { | ||
value.live = true; | ||
} | ||
}; | ||
const isLive = (peerJsId: string) => { | ||
const value = map.get(peerJsId); | ||
return value ? value.live : false; | ||
}; | ||
const hasConn = (peerJsId: string) => map.has(peerJsId); | ||
const delConn = (conn: Peer.DataConnection) => { | ||
const value = map.get(conn.peer); | ||
if (value && value.conn === conn) { | ||
map.delete(conn.peer); | ||
} | ||
}; | ||
const getLivePeerJsIds = () => | ||
Array.from(map.keys()).filter((k) => map.get(k)?.live); | ||
const forEachLiveConns = (callback: (conn: Peer.DataConnection) => void) => { | ||
for (const value of map.values()) { | ||
if (value.live) { | ||
callback(value.conn); | ||
} | ||
} | ||
}; | ||
return { | ||
addConn, | ||
markLive, | ||
isLive, | ||
hasConn, | ||
delConn, | ||
getLivePeerJsIds, | ||
forEachLiveConns, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters