react-peer-chat 0.1.2 → 0.1.3
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.js +13 -0
- package/build/index.js +49 -0
- package/build/useStorage.js +37 -0
- package/package.json +2 -1
- package/build/icons.jsx +0 -17
- package/build/index.jsx +0 -53
- package/build/useStorage.jsx +0 -45
package/build/icons.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export function BsFillMicFill(props) {
|
|
3
|
+
return React.createElement("span", Object.assign({}, props),
|
|
4
|
+
React.createElement("svg", { fill: "currentColor", width: '16px', height: '16px' },
|
|
5
|
+
React.createElement("path", { d: "M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z" }),
|
|
6
|
+
React.createElement("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
|
+
}
|
|
8
|
+
export function BsFillMicMuteFill(props) {
|
|
9
|
+
return React.createElement("span", Object.assign({}, props),
|
|
10
|
+
React.createElement("svg", { fill: "currentColor", width: '16px', height: '16px' },
|
|
11
|
+
React.createElement("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" }),
|
|
12
|
+
React.createElement("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" })));
|
|
13
|
+
}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import useStorage from './useStorage';
|
|
3
|
+
import { BsFillMicFill, BsFillMicMuteFill } from './icons';
|
|
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(() => {
|
|
10
|
+
if (!audio)
|
|
11
|
+
return;
|
|
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;
|
|
17
|
+
getUserMedia({
|
|
18
|
+
video: false,
|
|
19
|
+
audio: {
|
|
20
|
+
autoGainControl: false,
|
|
21
|
+
noiseSuppression: true,
|
|
22
|
+
echoCancellation: true
|
|
23
|
+
}
|
|
24
|
+
}, (stream) => {
|
|
25
|
+
localStream.current = stream;
|
|
26
|
+
call = peer.call(`rpc-${remotePeerId}`, stream);
|
|
27
|
+
call.on('stream', handleRemoteStream);
|
|
28
|
+
call.on('close', call.removeAllListeners);
|
|
29
|
+
peer.on('call', (e) => {
|
|
30
|
+
call = e;
|
|
31
|
+
call.answer(stream);
|
|
32
|
+
call.on('stream', handleRemoteStream);
|
|
33
|
+
call.on('close', call.removeAllListeners);
|
|
34
|
+
});
|
|
35
|
+
}, onError);
|
|
36
|
+
});
|
|
37
|
+
return () => {
|
|
38
|
+
var _a;
|
|
39
|
+
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(track => track.stop());
|
|
40
|
+
call === null || call === void 0 ? void 0 : call.removeAllListeners();
|
|
41
|
+
call === null || call === void 0 ? void 0 : call.close();
|
|
42
|
+
peer.removeAllListeners();
|
|
43
|
+
peer.destroy();
|
|
44
|
+
};
|
|
45
|
+
}, [audio]);
|
|
46
|
+
return React.createElement("button", { onClick: () => setAudio(audio => !audio) }, audio ? React.createElement(React.Fragment, null,
|
|
47
|
+
React.createElement("audio", { ref: streamRef, autoPlay: true, className: 'hidden' }),
|
|
48
|
+
React.createElement(BsFillMicFill, { title: "Turn mic off" })) : React.createElement(BsFillMicMuteFill, { title: "Turn mic on" }));
|
|
49
|
+
}
|
|
@@ -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.3",
|
|
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/icons.jsx
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
export function BsFillMicFill(props) {
|
|
3
|
-
return <span {...props}>
|
|
4
|
-
<svg fill="currentColor" viewBox="0 0 16 16">
|
|
5
|
-
<path d="M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z"/>
|
|
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
|
-
</svg>
|
|
8
|
-
</span>;
|
|
9
|
-
}
|
|
10
|
-
export function BsFillMicMuteFill(props) {
|
|
11
|
-
return <span {...props}>
|
|
12
|
-
<svg fill="currentColor" viewBox="0 0 16 16">
|
|
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
|
-
<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
|
-
</svg>
|
|
16
|
-
</span>;
|
|
17
|
-
}
|
package/build/index.jsx
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import React, { useEffect, useRef } from 'react';
|
|
2
|
-
import Peer from 'peerjs';
|
|
3
|
-
import useStorage from './useStorage';
|
|
4
|
-
import { BsFillMicFill, BsFillMicMuteFill } from './icons';
|
|
5
|
-
export default function Chat(_a) {
|
|
6
|
-
var peerId = _a.peerId, remotePeerId = _a.remotePeerId, peerOptions = _a.peerOptions, _b = _a.onError, onError = _b === void 0 ? function () { return console.error("Can not access microphone!"); } : _b;
|
|
7
|
-
var _c = useStorage('rpc-audio', false, { local: true, save: true }), audio = _c[0], setAudio = _c[1];
|
|
8
|
-
var streamRef = useRef(null);
|
|
9
|
-
var localStream = useRef();
|
|
10
|
-
var handleRemoteStream = function (remoteStream) { return streamRef.current.srcObject = remoteStream; };
|
|
11
|
-
useEffect(function () {
|
|
12
|
-
if (!audio)
|
|
13
|
-
return;
|
|
14
|
-
var peer = new Peer("rpc-".concat(peerId), peerOptions);
|
|
15
|
-
var call;
|
|
16
|
-
peer.on('open', function () {
|
|
17
|
-
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
18
|
-
getUserMedia({
|
|
19
|
-
video: false,
|
|
20
|
-
audio: {
|
|
21
|
-
autoGainControl: false,
|
|
22
|
-
noiseSuppression: true,
|
|
23
|
-
echoCancellation: true
|
|
24
|
-
}
|
|
25
|
-
}, function (stream) {
|
|
26
|
-
localStream.current = stream;
|
|
27
|
-
call = peer.call("rpc-".concat(remotePeerId), stream);
|
|
28
|
-
call.on('stream', handleRemoteStream);
|
|
29
|
-
call.on('close', call.removeAllListeners);
|
|
30
|
-
peer.on('call', function (e) {
|
|
31
|
-
call = e;
|
|
32
|
-
call.answer(stream);
|
|
33
|
-
call.on('stream', handleRemoteStream);
|
|
34
|
-
call.on('close', call.removeAllListeners);
|
|
35
|
-
});
|
|
36
|
-
}, onError);
|
|
37
|
-
});
|
|
38
|
-
return function () {
|
|
39
|
-
var _a;
|
|
40
|
-
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(function (track) { return track.stop(); });
|
|
41
|
-
call === null || call === void 0 ? void 0 : call.removeAllListeners();
|
|
42
|
-
call === null || call === void 0 ? void 0 : call.close();
|
|
43
|
-
peer.removeAllListeners();
|
|
44
|
-
peer.destroy();
|
|
45
|
-
};
|
|
46
|
-
}, [audio]);
|
|
47
|
-
return <button onClick={function () { return setAudio(function (audio) { return !audio; }); }}>
|
|
48
|
-
{audio ? <>
|
|
49
|
-
<audio ref={streamRef} autoPlay className='hidden'/>
|
|
50
|
-
<BsFillMicFill title="Turn mic off"/>
|
|
51
|
-
</> : <BsFillMicMuteFill title="Turn mic on"/>}
|
|
52
|
-
</button>;
|
|
53
|
-
}
|
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
|
-
}
|