peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

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

Open UTing1119 opened this issue 1 year ago • 3 comments
trafficstars

Please, check for existing issues to avoid duplicates.

  • [X] No similar issues found.

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

  • OS: windows 10 / window 11
  • Platform: nodeJS, Vue3
  • Browser: Chrome 126.0.6478.116 、 Chrome 126.0.6478.63

Is this a regression?

No response

Anything else?

No response

UTing1119 avatar Jun 26 '24 07:06 UTing1119

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

hissinger avatar Oct 13 '24 08:10 hissinger

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.

ogchefi avatar Oct 21 '24 13:10 ogchefi

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

UTing1119 avatar Oct 22 '24 06:10 UTing1119