WebSocket Secure localhost connection

2019-06-01 01:01发布

问题:

I've built a simple Node.js WebSocket chat server.

I can run it on localhost in a terminal tab.

In another terminal tab, I can connect to it with wscat, using ws://.

How do I connect to it on localhost with wss://?

回答1:

Look at this example code for how to establish an HTTPS server which will handle the TCP and TLS layers and then the ws module can handle the websocket aspects integrating with that server. You'll need a TLS certificate. For development you can generate your own and sign it yourself. A web search for "self-signed certificate" should lead you to the info you need for that part.