1. websocket 在热更新中的应用

1.1. 服务端

const express = require('express');
const app = express();
const WebSocketServer = require('ws').Server
const chokidar = require('chokidar')

wss = new WebSocketServer({ port: 8181 });
wss.on('connection', (ws) => {
  const watch = chokidar.watch('dist', {
    persistent: true,
    ignoreInitial: true,
    disableGlobbing: false
  })

  watch.on('change', (filePath) => {
    ws.send('reload')
  })
})

app.use('/', express.static('./dist'));

app.listen(3001);

chokidar 是基于 fs.watch 的文件监听第三方库

1.2. 客户端

const ws = new WebSocket('ws://localhost:8181');
ws.onmessage = function (e) {
  console.log('message', e.data)
  window.location.reload()
}
Copyright © tomgou 2022 all right reserved,powered by Gitbook该文章修订时间: 2023-08-28 17:33:23

results matching ""

    No results matching ""