react-peer-chat 0.11.3 → 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);
|
|
@@ -176,36 +186,42 @@ function useChat({
|
|
|
176
186
|
supports: { audioVideo, data }
|
|
177
187
|
}
|
|
178
188
|
}) => {
|
|
189
|
+
if (destroyed) return;
|
|
179
190
|
if (!data || !audioVideo) return onError(new Error("Browser not supported! Try some other browser."));
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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", () => {
|
|
184
197
|
resetConnections();
|
|
185
|
-
|
|
198
|
+
if (isMobile()) setPeerGeneration((prev) => prev + 1);
|
|
199
|
+
else peer.reconnect();
|
|
186
200
|
});
|
|
187
|
-
|
|
201
|
+
peer.on("error", (error) => {
|
|
188
202
|
if (error.type === "network" || error.type === "server-error") {
|
|
189
203
|
resetConnections();
|
|
190
|
-
setTimeout(() =>
|
|
204
|
+
setTimeout(() => {
|
|
205
|
+
if (isMobile()) setPeerGeneration((prev) => prev + 1);
|
|
206
|
+
else peer.reconnect();
|
|
207
|
+
}, 1e3);
|
|
191
208
|
onNetworkError == null ? void 0 : onNetworkError(error);
|
|
192
209
|
}
|
|
193
210
|
onPeerError(error);
|
|
194
211
|
});
|
|
195
|
-
if (destroyed) peer2.destroy();
|
|
196
|
-
else setPeer(peer2);
|
|
197
212
|
}
|
|
198
213
|
);
|
|
199
214
|
return () => {
|
|
215
|
+
var _a;
|
|
200
216
|
destroyed = true;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return void 0;
|
|
204
|
-
});
|
|
217
|
+
(_a = peerRef.current) == null ? void 0 : _a.destroy();
|
|
218
|
+
peerRef.current = null;
|
|
205
219
|
};
|
|
206
|
-
}, [completePeerId]);
|
|
220
|
+
}, [completePeerId, peerGeneration]);
|
|
207
221
|
useEffect(() => {
|
|
208
|
-
if (!text
|
|
222
|
+
if (!text) return;
|
|
223
|
+
const peer = peerRef.current;
|
|
224
|
+
if (!peer) return;
|
|
209
225
|
const connectData = () => completeRemotePeerIds.forEach((id) => handleConnection(peer.connect(id)));
|
|
210
226
|
if (peer.open) connectData();
|
|
211
227
|
peer.on("open", connectData);
|
|
@@ -213,9 +229,11 @@ function useChat({
|
|
|
213
229
|
peer.off("open", connectData);
|
|
214
230
|
resetConnections("data");
|
|
215
231
|
};
|
|
216
|
-
}, [text,
|
|
232
|
+
}, [text, peerEpoch]);
|
|
217
233
|
useEffect(() => {
|
|
218
|
-
if (!audio
|
|
234
|
+
if (!audio) return;
|
|
235
|
+
const peer = peerRef.current;
|
|
236
|
+
if (!peer) return;
|
|
219
237
|
const setupAudio = () => __async(null, null, function* () {
|
|
220
238
|
try {
|
|
221
239
|
localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
|
|
@@ -237,7 +255,7 @@ function useChat({
|
|
|
237
255
|
audioContextRef.current = null;
|
|
238
256
|
mixerRef.current = null;
|
|
239
257
|
};
|
|
240
|
-
}, [audio,
|
|
258
|
+
}, [audio, peerEpoch]);
|
|
241
259
|
return { peerId: completePeerId, remotePeers, messages, sendMessage, audio, setAudio };
|
|
242
260
|
}
|
|
243
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