react-peer-chat 0.1.2 → 0.1.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.
- package/build/{icons.jsx → icons.js} +2 -2
- package/build/{index.jsx → index.js} +17 -18
- package/build/useStorage.js +37 -0
- package/package.json +2 -1
- package/build/useStorage.jsx +0 -45
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
export function BsFillMicFill(props) {
|
|
3
3
|
return <span {...props}>
|
|
4
|
-
<svg fill="currentColor"
|
|
4
|
+
<svg fill="currentColor" width='16px' height='16px'>
|
|
5
5
|
<path d="M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z"/>
|
|
6
6
|
<path d="M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5z"/>
|
|
7
7
|
</svg>
|
|
@@ -9,7 +9,7 @@ export function BsFillMicFill(props) {
|
|
|
9
9
|
}
|
|
10
10
|
export function BsFillMicMuteFill(props) {
|
|
11
11
|
return <span {...props}>
|
|
12
|
-
<svg fill="currentColor"
|
|
12
|
+
<svg fill="currentColor" width='16px' height='16px'>
|
|
13
13
|
<path d="M13 8c0 .564-.094 1.107-.266 1.613l-.814-.814A4.02 4.02 0 0 0 12 8V7a.5.5 0 0 1 1 0v1zm-5 4c.818 0 1.578-.245 2.212-.667l.718.719a4.973 4.973 0 0 1-2.43.923V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 1 0v1a4 4 0 0 0 4 4zm3-9v4.879L5.158 2.037A3.001 3.001 0 0 1 11 3z"/>
|
|
14
14
|
<path d="M9.486 10.607 5 6.12V8a3 3 0 0 0 4.486 2.607zm-7.84-9.253 12 12 .708-.708-12-12-.708.708z"/>
|
|
15
15
|
</svg>
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import React, { useEffect, useRef } from 'react';
|
|
2
|
-
import Peer from 'peerjs';
|
|
3
2
|
import useStorage from './useStorage';
|
|
4
3
|
import { BsFillMicFill, BsFillMicMuteFill } from './icons';
|
|
5
|
-
export default function Chat(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
useEffect(function () {
|
|
4
|
+
export default function Chat({ peerId, remotePeerId, peerOptions, onError = () => console.error("Can not access microphone!") }) {
|
|
5
|
+
const [audio, setAudio] = useStorage('rpc-audio', false, { local: true, save: true });
|
|
6
|
+
const streamRef = useRef(null);
|
|
7
|
+
const localStream = useRef();
|
|
8
|
+
const handleRemoteStream = (remoteStream) => streamRef.current.srcObject = remoteStream;
|
|
9
|
+
useEffect(() => {
|
|
12
10
|
if (!audio)
|
|
13
11
|
return;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
const Peer = require('peerjs').default;
|
|
13
|
+
const peer = new Peer(`rpc-${peerId}`, peerOptions);
|
|
14
|
+
let call;
|
|
15
|
+
peer.on('open', () => {
|
|
16
|
+
const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
18
17
|
getUserMedia({
|
|
19
18
|
video: false,
|
|
20
19
|
audio: {
|
|
@@ -22,12 +21,12 @@ export default function Chat(_a) {
|
|
|
22
21
|
noiseSuppression: true,
|
|
23
22
|
echoCancellation: true
|
|
24
23
|
}
|
|
25
|
-
},
|
|
24
|
+
}, (stream) => {
|
|
26
25
|
localStream.current = stream;
|
|
27
|
-
call = peer.call(
|
|
26
|
+
call = peer.call(`rpc-${remotePeerId}`, stream);
|
|
28
27
|
call.on('stream', handleRemoteStream);
|
|
29
28
|
call.on('close', call.removeAllListeners);
|
|
30
|
-
peer.on('call',
|
|
29
|
+
peer.on('call', (e) => {
|
|
31
30
|
call = e;
|
|
32
31
|
call.answer(stream);
|
|
33
32
|
call.on('stream', handleRemoteStream);
|
|
@@ -35,16 +34,16 @@ export default function Chat(_a) {
|
|
|
35
34
|
});
|
|
36
35
|
}, onError);
|
|
37
36
|
});
|
|
38
|
-
return
|
|
37
|
+
return () => {
|
|
39
38
|
var _a;
|
|
40
|
-
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(
|
|
39
|
+
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(track => track.stop());
|
|
41
40
|
call === null || call === void 0 ? void 0 : call.removeAllListeners();
|
|
42
41
|
call === null || call === void 0 ? void 0 : call.close();
|
|
43
42
|
peer.removeAllListeners();
|
|
44
43
|
peer.destroy();
|
|
45
44
|
};
|
|
46
45
|
}, [audio]);
|
|
47
|
-
return <button onClick={
|
|
46
|
+
return <button onClick={() => setAudio(audio => !audio)}>
|
|
48
47
|
{audio ? <>
|
|
49
48
|
<audio ref={streamRef} autoPlay className='hidden'/>
|
|
50
49
|
<BsFillMicFill title="Turn mic off"/>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
const setStorage = (key, value, local = false) => (local ? localStorage : sessionStorage).setItem(key, JSON.stringify(value));
|
|
3
|
+
const removeStorage = (key, local = false) => (local ? localStorage : sessionStorage).removeItem(key);
|
|
4
|
+
const getStorage = (key, fallbackValue, local = false) => {
|
|
5
|
+
if (typeof window === "undefined")
|
|
6
|
+
return fallbackValue;
|
|
7
|
+
let value = (local ? localStorage : sessionStorage).getItem(key);
|
|
8
|
+
try {
|
|
9
|
+
if (!value)
|
|
10
|
+
throw new Error("Value doesn't exist");
|
|
11
|
+
value = JSON.parse(value);
|
|
12
|
+
}
|
|
13
|
+
catch (_a) {
|
|
14
|
+
if (fallbackValue !== undefined) {
|
|
15
|
+
value = fallbackValue;
|
|
16
|
+
setStorage(key, value, local);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
value = null;
|
|
20
|
+
removeStorage(key, local);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
export default function useStorage(key, initialValue, { local = false, save = false } = {}) {
|
|
26
|
+
save || (save = getStorage('mode') !== 'online');
|
|
27
|
+
const [storedValue, setStoredValue] = useState(save ? getStorage(key, initialValue, local) : initialValue);
|
|
28
|
+
const setValue = (value) => {
|
|
29
|
+
setStoredValue((old) => {
|
|
30
|
+
const updatedValue = typeof value === 'function' ? value(old) : value;
|
|
31
|
+
if (save)
|
|
32
|
+
setStorage(key, updatedValue, local);
|
|
33
|
+
return updatedValue;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
return [storedValue, setValue];
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-peer-chat",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "An easy to use react component for impleting peer-to-peer chatting.",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"module": "./build/index.js",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"react-dom": ">=16.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
+
"@types/node": "^20.5.9",
|
|
36
37
|
"@types/react": "^18.2.21"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
package/build/useStorage.jsx
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
|
-
var setStorage = function (key, value, local) {
|
|
3
|
-
if (local === void 0) { local = false; }
|
|
4
|
-
return (local ? localStorage : sessionStorage).setItem(key, JSON.stringify(value));
|
|
5
|
-
};
|
|
6
|
-
var removeStorage = function (key, local) {
|
|
7
|
-
if (local === void 0) { local = false; }
|
|
8
|
-
return (local ? localStorage : sessionStorage).removeItem(key);
|
|
9
|
-
};
|
|
10
|
-
var getStorage = function (key, fallbackValue, local) {
|
|
11
|
-
if (local === void 0) { local = false; }
|
|
12
|
-
if (typeof window === "undefined")
|
|
13
|
-
return fallbackValue;
|
|
14
|
-
var value = (local ? localStorage : sessionStorage).getItem(key);
|
|
15
|
-
try {
|
|
16
|
-
if (!value)
|
|
17
|
-
throw new Error("Value doesn't exist");
|
|
18
|
-
value = JSON.parse(value);
|
|
19
|
-
}
|
|
20
|
-
catch (_a) {
|
|
21
|
-
if (fallbackValue !== undefined) {
|
|
22
|
-
value = fallbackValue;
|
|
23
|
-
setStorage(key, value, local);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
value = null;
|
|
27
|
-
removeStorage(key, local);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
};
|
|
32
|
-
export default function useStorage(key, initialValue, _a) {
|
|
33
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.local, local = _c === void 0 ? false : _c, _d = _b.save, save = _d === void 0 ? false : _d;
|
|
34
|
-
save || (save = getStorage('mode') !== 'online');
|
|
35
|
-
var _e = useState(save ? getStorage(key, initialValue, local) : initialValue), storedValue = _e[0], setStoredValue = _e[1];
|
|
36
|
-
var setValue = function (value) {
|
|
37
|
-
setStoredValue(function (old) {
|
|
38
|
-
var updatedValue = typeof value === 'function' ? value(old) : value;
|
|
39
|
-
if (save)
|
|
40
|
-
setStorage(key, updatedValue, local);
|
|
41
|
-
return updatedValue;
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
return [storedValue, setValue];
|
|
45
|
-
}
|