admin管理员组文章数量:1422437
When peer 1 connects to peer 2
- the highlighted code in the picture should fire
- peer 2 should send peer 1 "hello!"
- peer 1 should have "hello!" printed in its console
Peer 1 connects to peer 2
Problem: peer 1 does not have "hello!" printed in its console
// make a new peer
const peer = new Peer('', {
host: '/',
port: '3001'
});
// "connection" event fires when someone tries to connect to us
peer.on('connection', (conn) => {
console.log('someone connected');
// "data" event fires when someone sends us a message
conn.on('data', (data) => {
console.log(data);
});
// ===========================================================================
// Problem: Both Attempt 1 and Attempt 2 fail to run
// ATTEMPT 1: "open" event fires when the connection is opened
conn.on('open', () => {
conn.send('hello!');
});
// ATTEMPT 2:
conn.send('hello!');
// ===========================================================================
});
// connect to a peer
const conn = peer.connect('another-peers-id');
// after connecting to peer, send "hi" to them
conn.on('open', () => {
conn.send('hi!');
});
When peer 1 connects to peer 2
- the highlighted code in the picture should fire
- peer 2 should send peer 1 "hello!"
- peer 1 should have "hello!" printed in its console
Peer 1 connects to peer 2
Problem: peer 1 does not have "hello!" printed in its console
// make a new peer
const peer = new Peer('', {
host: '/',
port: '3001'
});
// "connection" event fires when someone tries to connect to us
peer.on('connection', (conn) => {
console.log('someone connected');
// "data" event fires when someone sends us a message
conn.on('data', (data) => {
console.log(data);
});
// ===========================================================================
// Problem: Both Attempt 1 and Attempt 2 fail to run
// ATTEMPT 1: "open" event fires when the connection is opened
conn.on('open', () => {
conn.send('hello!');
});
// ATTEMPT 2:
conn.send('hello!');
// ===========================================================================
});
// connect to a peer
const conn = peer.connect('another-peers-id');
// after connecting to peer, send "hi" to them
conn.on('open', () => {
conn.send('hi!');
});
Share
Improve this question
edited Jan 12, 2021 at 2:08
asked Jan 11, 2021 at 19:58
user13585926user13585926
3 Answers
Reset to default 4Actually, there might be a problem with the connection itself.
After you do this
const peer = new Peer('', {
host: '/',
port: '3001'
});
a new peer object is created and retured but, socket takes a bit of time to open up. You can check this if you set the debug to 3,
If connect function is called before getting socket open, connect
will fail
const conn = peer.connect('another-peers-id');
You can try add a setTimeout
for connect
to test this theory.
setTimeout(() => {
const conn = peer.connect('id');
}, 1000);
And same problem exists with 'connection'
callback also
You can add setTimeout inside connection callback too
peer.on("connection", (conn) => {
console.log("Connected to: " + conn.peer);
setTimeout(() => {
...enter code here...
}
)
The solution is to wrap everything into peer.on('open', ...)
like this:
peer.on('open', () => {
const conn = peer.connect('id-of-host')
})
you have to set a server side for you PeerJS
run a free cloud-hosted version of PeerServer for testing , just change the
const peer = new Peer('', {
host: '/',
port: '3001'
});
to
var MyPeerId ;
const peer = new Peer();
and to get an id use :
peer.on('open', function(){
MyPeerId = peer.id;
});
//this is an async function so you have be sure that you got an id before connection to someone else
//rest of your code here
function Connect(OtherPeerId){
if(!MyPeerId)
return 0; // you didn't get a peerid yet
//rest of your code
}
本文标签: javascriptPeerJSconnectionon(39open39) not executingStack Overflow
版权声明:本文标题:javascript - PeerJS - connection.on('open') not executing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745357346a2655133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论