![]() |
#2
MaDaO_062023-07-16 01:05
|

var http = require('http');
var fs = require('fs');
http.createServer(function (request, response) {
if(request.url === "/") {
fs.readFile('./test.html', (err, data) => {
response.end(data)
})
} else if(request.url === "/test.js") {
fs.readFile('./test.js', (err, data) => {
response.end(data)
})
} else if(request.url === "/worker.js") {
console.log(111)
fs.readFile('./worker.js', (err, data) => {
response.end(data)
})
} else {
response.end("hello")
}
}).listen(8888);
var fs = require('fs');
http.createServer(function (request, response) {
if(request.url === "/") {
fs.readFile('./test.html', (err, data) => {
response.end(data)
})
} else if(request.url === "/test.js") {
fs.readFile('./test.js', (err, data) => {
response.end(data)
})
} else if(request.url === "/worker.js") {
console.log(111)
fs.readFile('./worker.js', (err, data) => {
response.end(data)
})
} else {
response.end("hello")
}
}).listen(8888);
test.html:
<script src="test.js"></script>
test.js:
let worker = new SharedWorker('worker.js')
worker.port.postMessage("hello")
worker.port.postMessage("hello")
worker.js

onconnect = (e) => {
const port = e.ports[0]
port.onmessage = (e) => {
console.log(e.data)
}
}
const port = e.ports[0]
port.onmessage = (e) => {
console.log(e.data)
}
}
访问 http://localhost:8888,然后浏览器控制台 Network 里 worker.js 一直在pending,是什么问题