aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/fixtures/peertube-plugin-test-websocket/main.js
blob: 3fde76cfe95b81a413f64c183d537b12db14a192 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const WebSocketServer = require('ws').WebSocketServer

async function register ({
  registerWebSocketRoute
}) {
  const wss = new WebSocketServer({ noServer: true })

  wss.on('connection', function connection(ws) {
    ws.on('message', function message(data) {
      if (data.toString() === 'ping') {
        ws.send('pong')
      }
    })
  })

  registerWebSocketRoute({
    route: '/toto',

    handler: (request, socket, head) => {
      wss.handleUpgrade(request, socket, head, ws => {
        wss.emit('connection', ws, request)
      })
    }
  })
}

async function unregister () {
  return
}

module.exports = {
  register,
  unregister
}

// ###########################################################################