peers / peerjs-server

Server for PeerJS

Home Page:https://peerjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: add `createWebSocketServer` option

redexp opened this issue · comments

I have a suggestion:

I have few websocket servers on one same port 443. It's because of some companies security rules that restrict access to non default ports. How I handle it - each server has it own "handleUpgrade" method and my code looks like this

httpServer.on('upgrade', (req, socket, head) => {
  if (req.url === '/server-1') {
    ws1.handleUpgrade(req, socket, head);
  }
  else if (req.url === '/server-2') {
    ws2.handleUpgrade(req, socket, head);
  }
})

Now I need to add your peer server, but I can't integrate it in this code because your ws is deep in the code and not exposed to public. And here my suggestion - add createWebSocketServer option so I can fully control all of it's options and how it get connected to my http server, for example

const {Server} = require('ws');

ExpressPeerServer(httpServer, {
  createWebSocketServer: () => {
    const ws = new Server({
      noServer: true
    });
    httpServer.on('upgrade', (req, socket, head) => {
      if (req.url === '/peer') {
        ws.handleUpgrade(req, socket, head);
      }
    });
    return ws;
  }
});

Also this option can give me ability to use something on top of ws library but with the same api.

If you ok with this code then I can create pull request

That sounds like a good idea! Please, go ahead with the pull request.

@jonasgloning can you please review my pull request?

Thanks @redexp for the reminder; sorry it took so long!
I have two tiny nitpicks; let me know if you want to address them or if I should just change them while merging.