[英]why in javascript i cant create object in this function?
i have a problem with creating object inside my function i know its a void function but i don't need to return any data我在函数内创建对象时遇到问题我知道它是一个 void 函数,但我不需要返回任何数据
let btnConnect = document.getElementById('connect')
let btnConnectedDrive = document.getElementById('ConnectedDrive')
function createPeer() {
let myPeer = new Peer()
console.log(myPeer.id)
let socket = new WebSocket('ws://' + window.location.host + '/ws/myapp/room/' + game_id + '/');
socket.onopen = () => socket.send(JSON.stringify(
{
'username': user,
'PeerId': myPeer.id
}
),)
}
btnConnect.onclick = () => createPeer();
now when i log myPeer.id its null
and same on the server what do you think solution is ?现在,当我在服务器上记录 myPeer.id 它的null
和相同时,您认为解决方案是什么? hope you have a very nice time .希望你过得愉快。
See the documentation :请参阅文档:
Other peers can connect to this peer using the provided ID.其他对等点可以使用提供的 ID 连接到该对等点。 If no ID is given, one will be generated by the brokering server .如果没有给出 ID,将由代理服务器生成一个。
And the example:还有这个例子:
peer.on('open', function(id) { console.log('My peer ID is: ' + id); });
It doesn't have an ID because you haven't waited for it to connect to the brokering server and be issued with one.它没有 ID,因为您没有等待它连接到代理服务器并获得 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.