2

I want to be able to programmatically disconnect user if the client.id and client.token does seems fishy (PeerServer).

How would I go about doing that inside the connection handler -

peerServer.on('connection', (client) => {
    if(is_fishy(client)) {
        // disconnected it.
    }
});
ShadowDoom
  • 94
  • 1
  • 5

1 Answers1

0

The .on('connection') event passes a DataConnection object that you can use to disconnect a remote peer.

peerServer.on('connection', (client) => {
  if (is_fishy(client)) {
    client.close();
  }
});
Brian Burton
  • 3,648
  • 16
  • 29
  • This code doesnt clean up stuff. Might create a memory leak on a production server. I saw this - https://stackoverflow.com/questions/32464596/disconnect-a-peer-from-peerjs-on-the-server-side/32465401#32465401 However, the code doesn't make much sense probably because of version differences. – ShadowDoom Feb 18 '21 at 14:05
  • The above is a specific answer to your specific question. For "clean up stuff" you'd need to post a separate question with code samples of what needs to be cleaned up. The linked example looks like he's storing connection metadata in objects manually, which wouldn't apply to the few lines of code you posted. – Brian Burton Feb 18 '21 at 14:10
  • I know but this this question is specific to "peerjs" and "peer". I am aware that websockets can be closed like that. – ShadowDoom Feb 18 '21 at 17:16
  • I'm afraid that I don't understand what you're looking for, the above is a PeerJS answer to a PeerJS question. Hope someone else can help you out. – Brian Burton Feb 18 '21 at 17:28
  • A peerJS answer would provide the clean up code also. Otherwise its not peerJS answer. Rather a websocket answer. :-) – ShadowDoom Feb 23 '21 at 08:23