react-peer-chat 0.11.2 → 0.11.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useChat } from './chunk-
|
|
1
|
+
import { useChat } from './chunk-UFHA4GH3.js';
|
|
2
2
|
import { BiSolidMessageX, BiSolidMessageDetail, GrSend, BsFillMicFill, BsFillMicMuteFill } from './chunk-QIPTWGEX.js';
|
|
3
3
|
import { __objRest, __spreadValues } from './chunk-FZ4QVG4I.js';
|
|
4
4
|
import React, { useRef, useState, useEffect } from 'react';
|
|
@@ -31,6 +31,8 @@ var defaults = {
|
|
|
31
31
|
peerOptions: {},
|
|
32
32
|
remotePeerId: []
|
|
33
33
|
};
|
|
34
|
+
var iosRegex = /iPhone|iPad|iPod/i;
|
|
35
|
+
var mobileRegex = /Android|webOS|BlackBerry|IEMobile|Opera Mini/i;
|
|
34
36
|
|
|
35
37
|
// src/lib/connection.ts
|
|
36
38
|
function closeConnection(conn) {
|
|
@@ -40,6 +42,12 @@ function closeConnection(conn) {
|
|
|
40
42
|
|
|
41
43
|
// src/lib/utils.ts
|
|
42
44
|
var addPrefix = (str) => `rpc-${str}`;
|
|
45
|
+
function isMobile(iOS = true) {
|
|
46
|
+
var _a;
|
|
47
|
+
let result = (_a = navigator.userAgentData) == null ? void 0 : _a.mobile;
|
|
48
|
+
result != null ? result : result = mobileRegex.test(navigator.userAgent) || iOS && iosRegex.test(navigator.userAgent);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
43
51
|
|
|
44
52
|
// src/lib/react.ts
|
|
45
53
|
function isSetStateFunction(v) {
|
|
@@ -62,8 +70,10 @@ function useChat({
|
|
|
62
70
|
onMessageSent,
|
|
63
71
|
onMessageReceived
|
|
64
72
|
}) {
|
|
65
|
-
const [
|
|
73
|
+
const [peerEpoch, setPeerEpoch] = useState(0);
|
|
74
|
+
const [peerGeneration, setPeerGeneration] = useState(0);
|
|
66
75
|
const [audio, setAudio] = useAudio(allowed);
|
|
76
|
+
const peerRef = useRef(null);
|
|
67
77
|
const connRef = useRef({});
|
|
68
78
|
const callsRef = useRef({});
|
|
69
79
|
const localStreamRef = useRef(null);
|
|
@@ -95,8 +105,9 @@ function useChat({
|
|
|
95
105
|
}
|
|
96
106
|
function handleConnection(conn) {
|
|
97
107
|
const peerId2 = conn.peer;
|
|
98
|
-
if (connRef.current[peerId2]) return conn.close();
|
|
99
108
|
conn.on("open", () => {
|
|
109
|
+
var _a;
|
|
110
|
+
(_a = connRef.current[peerId2]) == null ? void 0 : _a.close();
|
|
100
111
|
connRef.current[peerId2] = conn;
|
|
101
112
|
conn.on("data", ({ type, message, messages: messages2, remotePeerName }) => {
|
|
102
113
|
switch (type) {
|
|
@@ -113,17 +124,18 @@ function useChat({
|
|
|
113
124
|
});
|
|
114
125
|
conn.on("close", () => {
|
|
115
126
|
conn.removeAllListeners();
|
|
116
|
-
delete connRef.current[peerId2];
|
|
127
|
+
if (connRef.current[peerId2] === conn) delete connRef.current[peerId2];
|
|
117
128
|
});
|
|
118
129
|
}
|
|
119
130
|
function handleCall(call) {
|
|
120
131
|
const peerId2 = call.peer;
|
|
121
|
-
if (callsRef.current[peerId2]) return call.close();
|
|
122
132
|
if (!call.localStream) {
|
|
123
133
|
if (!localStreamRef.current) return call.close();
|
|
124
134
|
call.answer(localStreamRef.current);
|
|
125
135
|
}
|
|
126
136
|
call.on("stream", () => {
|
|
137
|
+
var _a;
|
|
138
|
+
(_a = callsRef.current[peerId2]) == null ? void 0 : _a.close();
|
|
127
139
|
callsRef.current[peerId2] = call;
|
|
128
140
|
if (!audioContextRef.current) audioContextRef.current = new AudioContext();
|
|
129
141
|
if (audioContextRef.current.state === "suspended") audioContextRef.current.resume();
|
|
@@ -142,8 +154,10 @@ function useChat({
|
|
|
142
154
|
});
|
|
143
155
|
call.on("close", () => {
|
|
144
156
|
call.removeAllListeners();
|
|
145
|
-
|
|
146
|
-
|
|
157
|
+
if (callsRef.current[peerId2] === call) {
|
|
158
|
+
removePeerAudio(peerId2);
|
|
159
|
+
delete callsRef.current[peerId2];
|
|
160
|
+
}
|
|
147
161
|
});
|
|
148
162
|
}
|
|
149
163
|
function receiveMessage(message) {
|
|
@@ -172,36 +186,42 @@ function useChat({
|
|
|
172
186
|
supports: { audioVideo, data }
|
|
173
187
|
}
|
|
174
188
|
}) => {
|
|
189
|
+
if (destroyed) return;
|
|
175
190
|
if (!data || !audioVideo) return onError(new Error("Browser not supported! Try some other browser."));
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
191
|
+
peerRef.current = new Peer(completePeerId, __spreadValues({ config: defaultConfig }, peerOptions));
|
|
192
|
+
setPeerEpoch((prev) => prev + 1);
|
|
193
|
+
const peer = peerRef.current;
|
|
194
|
+
peer.on("connection", handleConnection);
|
|
195
|
+
peer.on("call", handleCall);
|
|
196
|
+
peer.on("disconnected", () => {
|
|
180
197
|
resetConnections();
|
|
181
|
-
|
|
198
|
+
if (isMobile()) setPeerGeneration((prev) => prev + 1);
|
|
199
|
+
else peer.reconnect();
|
|
182
200
|
});
|
|
183
|
-
|
|
201
|
+
peer.on("error", (error) => {
|
|
184
202
|
if (error.type === "network" || error.type === "server-error") {
|
|
185
203
|
resetConnections();
|
|
186
|
-
setTimeout(() =>
|
|
204
|
+
setTimeout(() => {
|
|
205
|
+
if (isMobile()) setPeerGeneration((prev) => prev + 1);
|
|
206
|
+
else peer.reconnect();
|
|
207
|
+
}, 1e3);
|
|
187
208
|
onNetworkError == null ? void 0 : onNetworkError(error);
|
|
188
209
|
}
|
|
189
210
|
onPeerError(error);
|
|
190
211
|
});
|
|
191
|
-
if (destroyed) peer2.destroy();
|
|
192
|
-
else setPeer(peer2);
|
|
193
212
|
}
|
|
194
213
|
);
|
|
195
214
|
return () => {
|
|
215
|
+
var _a;
|
|
196
216
|
destroyed = true;
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return void 0;
|
|
200
|
-
});
|
|
217
|
+
(_a = peerRef.current) == null ? void 0 : _a.destroy();
|
|
218
|
+
peerRef.current = null;
|
|
201
219
|
};
|
|
202
|
-
}, [completePeerId]);
|
|
220
|
+
}, [completePeerId, peerGeneration]);
|
|
203
221
|
useEffect(() => {
|
|
204
|
-
if (!text
|
|
222
|
+
if (!text) return;
|
|
223
|
+
const peer = peerRef.current;
|
|
224
|
+
if (!peer) return;
|
|
205
225
|
const connectData = () => completeRemotePeerIds.forEach((id) => handleConnection(peer.connect(id)));
|
|
206
226
|
if (peer.open) connectData();
|
|
207
227
|
peer.on("open", connectData);
|
|
@@ -209,9 +229,11 @@ function useChat({
|
|
|
209
229
|
peer.off("open", connectData);
|
|
210
230
|
resetConnections("data");
|
|
211
231
|
};
|
|
212
|
-
}, [text,
|
|
232
|
+
}, [text, peerEpoch]);
|
|
213
233
|
useEffect(() => {
|
|
214
|
-
if (!audio
|
|
234
|
+
if (!audio) return;
|
|
235
|
+
const peer = peerRef.current;
|
|
236
|
+
if (!peer) return;
|
|
215
237
|
const setupAudio = () => __async(null, null, function* () {
|
|
216
238
|
try {
|
|
217
239
|
localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
|
|
@@ -233,7 +255,7 @@ function useChat({
|
|
|
233
255
|
audioContextRef.current = null;
|
|
234
256
|
mixerRef.current = null;
|
|
235
257
|
};
|
|
236
|
-
}, [audio,
|
|
258
|
+
}, [audio, peerEpoch]);
|
|
237
259
|
return { peerId: completePeerId, remotePeers, messages, sendMessage, audio, setAudio };
|
|
238
260
|
}
|
|
239
261
|
function useMessages() {
|
package/dist/components.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Chat as default } from './chunks/chunk-
|
|
2
|
-
import './chunks/chunk-
|
|
1
|
+
export { Chat as default } from './chunks/chunk-HNBONO2J.js';
|
|
2
|
+
import './chunks/chunk-UFHA4GH3.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
4
|
import './chunks/chunk-ZYFPSCFE.js';
|
|
5
5
|
import './chunks/chunk-FZ4QVG4I.js';
|
package/dist/hooks.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Chat as default } from './chunks/chunk-
|
|
2
|
-
export { useChat } from './chunks/chunk-
|
|
1
|
+
export { Chat as default } from './chunks/chunk-HNBONO2J.js';
|
|
2
|
+
export { useChat } from './chunks/chunk-UFHA4GH3.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
4
|
export { clearChat } from './chunks/chunk-ZYFPSCFE.js';
|
|
5
5
|
import './chunks/chunk-FZ4QVG4I.js';
|
package/package.json
CHANGED