A
A
Alex Goncharov2020-07-02 21:41:46
Node.js
Alex Goncharov, 2020-07-02 21:41:46

How to use WSS instead of WS in Node.js?

Now I'm getting errors in the console. https://skr.sh/s2xCgPaykjE What's the problem? Need to install some module and enable it?

var hat = require('hat')
var http = require('http')
var nodeStatic = require('node-static')
var ws = require('ws')

var PORT = process.argv[2] || 3000

var httpServer = http.createServer()
var staticServer = new nodeStatic.Server('./public')
var wsServer = new ws.Server({ server: httpServer })

var peers = {}
var waitingId = null
var count = 0

httpServer.on('request', function (req, res) {
  req.addListener('end', function () {
    staticServer.serve(req, res)
  }).resume()
})

wsServer.on('connection', onconnection)

function onconnection (peer) {
  var send = peer.send
  peer.send = function () {
    try {
      send.apply(peer, arguments)
    } catch (err) {}
  }

  peer.id = hat()
  peers[peer.id] = peer
  peer.on('close', onclose.bind(peer))
  peer.on('error', onclose.bind(peer))
  peer.on('message', onmessage.bind(peer))
  count += 1
  broadcast(JSON.stringify({ type: 'count', data: count }))
}

function onclose () {
  peers[this.id] = null
  if (this.id === waitingId) {
    waitingId = null
  }
  if (this.peerId) {
    var peer = peers[this.peerId]
    peer.peerId = null
    peer.send(JSON.stringify({ type: 'end' }), onsend)
  }
  count -= 1
  broadcast(JSON.stringify({ type: 'count', data: count }))
}

function onmessage (data) {
  console.log('[' + this.id + ' receive] ' + data + '\n')
  try {
    var message = JSON.parse(data)
  } catch (err) {
    console.error('Discarding non-JSON message: ' + err)
    return
  }

  if (message.type === 'peer') {
    if (waitingId && waitingId !== this.id) {
      var peer = peers[waitingId]

      this.peerId = peer.id
      peer.peerId = this.id

      this.send(JSON.stringify({
        type: 'peer',
        data: {
          initiator: true
        }
      }), onsend)

      peer.send(JSON.stringify({
        type: 'peer'
      }), onsend)

      waitingId = null
    } else {
      waitingId = this.id
    }
  } else if (message.type === 'signal') {
    if (!this.peerId) return console.error('unexpected `signal` message')
    var peer = peers[this.peerId]
    peer.send(JSON.stringify({ type: 'signal', data: message.data }))
  } else if (message.type === 'end') {
    if (!this.peerId) return console.error('unexpected `end` message')
    var peer = peers[this.peerId]
    peer.peerId = null
    this.peerId = null
    peer.send(JSON.stringify({ type: 'end' }), onsend)
  } else {
    console.error('unknown message `type` ' + message.type)
  }
}

function onsend (err) {
  if (err) console.error(err.stack || err.message || err)
}

function broadcast (message) {
  for (var id in peers) {
    var peer = peers[id]
    if (peer) {
      peer.send(message)
    }
  }
}

httpServer.listen(PORT, function () {
  console.log('Listening on port ' + PORT)
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cython, 2020-07-02
@cython

WSS - WS + SSL. That is, you need to use https server instead of http.
An example of such a server:

const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');

const server = https.createServer({
  cert: fs.readFileSync('./cert.pem'),
  key: fs.readFileSync('./key.key')
}, (req, res) => {
  console.log("Request");
  res.end("Nice");
});

const wss = new WebSocket.Server({ server });

wss.on('connection', (ws) => {
  ws.on('message', (msg) => {
    console.log(msg);
    ws.send(`msg: ${msg}`);
  });
  console.log("connected");
  ws.send('connected');
});

server.listen(443);

Also, since you are using HTTPS, you need an SSL certificate, for test purposes you can create it yourself (easy to google). If you are going to put it somewhere, then you need a signed certificate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question