peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.48k stars 1.43k forks source link

peer.reconnect sometimes can't reconnect to server, and won't fire any event #1289

Open UTing1119 opened 4 months ago

UTing1119 commented 4 months ago

Please, check for existing issues to avoid duplicates.

What happened?

hello, I'm using Vue 3 with PeerJS.

"peerjs": "^1.5.4",
"vue": "^3.4.27",
"vite": "^2.9.14",
node version: 18.3.0

I'm creating a application and using free PeerJS server to share screen or camera. while testing, I try to cut one PC's WiFi down and reopen the WiFi after disconnect event is fired. But reconnect only works sometimes. after I call reconnect, nothing happens. No event, no connect, no error.

the status which PeerJS offer, like opendestory will stay false. Only disconnect will change to false after I call reconnect. If I try to recoonect it again, the result wont change.

after I open the debug of server, it shows the steps and we can see it's not get the message:

"try to reconnect..."

PeerJS: Attempting reconnection to server with ID ****
PeerJS: Socket open
// end, no next one

it should be like this

"try to reconnect..."

PeerJS: Attempting reconnection to server with ID ****
PeerJS: Socket open
PeerJS: Server message received: { type: 'OPEN' } // we're missing this one
// next: fire the open event

here is my code

let peerConnection = new Peer()
let reconnectInterval: any
peerConnection.on('open', () => {
  console.log('open')
  if (reconnectInterval) {
    window.clearInterval(reconnectInterval)
    reconnectInterval = undefined
  }
})
peerConnection.on('error', (e) => {
  console.error('peer error, error = ' + e)
})
peerConnection.on('disconnected', (e) => {
  console.error('peer disconnected, error = ' + e + ', ' + new Date())
  if (!reconnectInterval) {
    reconnectInterval = setInterval(() => {
      if (!peerConnection.destroyed && !peerConnection.open) {
        console.log('try to reconnect...')
        try {
          peerConnection.reconnect()
        } catch (e) {
          console.error('reconnect error, error = ' + e)
        }
      }
    }, 5000)
  }
})

does anyone know about this?

How can we reproduce the issue?

No response

What do you expected to happen?

if can't reconnect, it will fire event to try again, and return evey error or fire event.

Environment setup

Is this a regression?

No response

Anything else?

No response

hissinger commented 1 month ago

hi

I had the same issue.

When a new socket connects before the previous socket is removed during reconnect(), the OPEN message was not sent.

I submitted a PR to the PeerJS server repo. https://github.com/peers/peerjs-server/pull/462

ogchefi commented 3 weeks ago

PR fix by @hissinger appears to work for me. I was just dealing with this exact issue and wondering why nothing gets called after reconnect. I noticed this when I was switching between wifi networks and checking how my app reacts to it.

UTing1119 commented 3 weeks ago

thanks a lot!! hope they will accept the PR soon