First create a self-signed certificate using OpenSSL:
1024x768 >/path/to/private. PEM//-new -key/path/to/ Private. Pem-out csr.pem//365 - in Csr.pem- signkey/path/to/Private. PEM- out/PATH/TO/FILE.CRT
To create a WSS server using the WS module:
varHttps=require ('HTTPS');varWs=require ('ws');varFs=require ('FS');varKEYPATH=PROCESS.CWD () +'/server.key';//I put the key file in the directory running the command to testvarCERTPATH=PROCESS.CWD () +'/SERVER.CRT';//Console.log (keypath);//Console.log (certpath); varOptions ={Key:fs.readFileSync (keypath), Cert:fs.readFileSync (Certpath), passphrase:'1234'//if the secret key file has a password, use this property to set the password}; varServer=https.createserver (Options, function (req, res) {//If you have a simple HTTPS connection, you're going to return this thing .Res.writehead (403);//403 canRes.end ("This is a WebSockets server!\n");}). Listen (15449); varWSS =NewWs. Server ({server:server});//throw the created HTTPS server into WebSocket's creation function, and WS will use this server to create the WSS service .//Similarly, if an HTTP service is thrown in, then the WS-Service is created without encryptionWss.on ('Connection', Function (wsconnect) {Wsconnect.on ('message', function (message) {console.log (message); });});
Client Links:
var New WebSocket ('wss://localhost:15449/', { 8, ' https://localhost:15449 ' , false//important, the self-signed certificate can only be set. A trusted certificate issued by a CA is not required });
Nodejs to build a WSS server