我正在尝试使用简单的对等点打开数据通道。我使用 Firebase 发送信号,并且能够启动连接功能。
const [thisPeer, setThisPeer] = useState();
const [connectionStatus, setConnectionStatus] = useState(false);
// in a function
var thisPeerT = new Peer({
initiator: false,
config: {
iceServers: iceServers,
},
});
// signaling code
thisPeerT.on("connect", function (data) {
console.log("onconnect");
console.log(data);
setConnectionStatus(true);
});
setThisPeer(thisPeerT);
在连接时,我设置了一个状态钩子,该钩子会触发使用效果,在其中创建我的连接数据。
useEffect(
function () {
console.log("here");
console.log(roomID);
console.log(thisPeer);
console.log(connectionStatus);
if (roomID && thisPeer && connectionStatus) {
console.log("here2");
thisPeer.on("data", function (data) {
console.log(data);
});
}
},
[roomID, thisPeer, connectionStatus]
);
但是,当我尝试使用 thisPeer.on("data"...
时,出现以下错误here
XXYDCgqle4XvZDZ6gm6
Peer {_readableState: ReadableState, readable: true, _events: {…}, _eventsCount: 4, _maxListeners: undefined, …}
true
here2
Uncaught ReferenceError: process is not defined
at resume (_stream_readable.js:905:1)
at Peer.Readable.resume (_stream_readable.js:895:1)
at Peer.Readable.on (_stream_readable.js:813:1)
at ConnectionContext.js:142:1
at invokePassiveEffectCreate (react-dom.development.js:23487:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
at invokeGuardedCallback (react-dom.development.js:4056:1)
at flushPassiveEffectsImpl (react-dom.development.js:23574:1)
at unstable_runWithPriority (scheduler.development.js:468:1)
我尝试将数据放在整个代码中的各个位置,无论位置如何,似乎都会发生此错误。如果我在另一个对等点上调用peer.send(),它还会在接收数据的对等点上给出进程未定义错误。有谁知道我如何解决该错误或我应该在哪里打开数据通道?
我遇到了同样的问题,但我可以使用 CDN 脚本来解决问题
<script src="https://cdnjs.cloudflare.com/ajax/libs/simple-peer/9.11.1/simplepeer.min.js"></script>
这里是如何在 React 中调用它的代码示例
var peer1 = new window.SimplePeer({ initiator: true })
基于this帖子,请确保创建一个文件,例如
./src/setup.js
或./src/process.js
其中包括以下内容:
import process from 'process';
global = window;
process = process;
Buffer = [];
进入
./src/index.js
并使用:
import './[your-file]';
... Other imports & the rest of the code ...
重新启动应用程序,它应该可以工作!