index.js (779B)
1 'use strict'; 2 3 const express = require('express'); 4 const path = require('path'); 5 const { createServer } = require('http'); 6 7 const { WebSocketServer } = require('../..'); 8 9 const app = express(); 10 app.use(express.static(path.join(__dirname, '/public'))); 11 12 const server = createServer(app); 13 const wss = new WebSocketServer({ server }); 14 15 wss.on('connection', function (ws) { 16 const id = setInterval(function () { 17 ws.send(JSON.stringify(process.memoryUsage()), function () { 18 // 19 // Ignore errors. 20 // 21 }); 22 }, 100); 23 console.log('started client interval'); 24 25 ws.on('close', function () { 26 console.log('stopping client interval'); 27 clearInterval(id); 28 }); 29 }); 30 31 server.listen(8080, function () { 32 console.log('Listening on http://localhost:8080'); 33 });