mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-11 09:39:10 +00:00
33 lines
971 B
HTML
33 lines
971 B
HTML
![]() |
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>go-wasm-http-server hello demo</title>
|
||
|
<script>
|
||
|
navigator.serviceWorker.register('sw.js')
|
||
|
.then(registration => {
|
||
|
const serviceWorker = registration.installing ?? registration.waiting ?? registration.active
|
||
|
if (serviceWorker.state === 'activated') {
|
||
|
startEventSource()
|
||
|
} else {
|
||
|
serviceWorker.addEventListener('statechange', e => {
|
||
|
if (e.target.state === 'activated') startEventSource()
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
|
||
|
function startEventSource() {
|
||
|
const es = new EventSource('/api/events')
|
||
|
es.addEventListener('ping', (e) => {
|
||
|
const p = document.createElement('p')
|
||
|
p.textContent = `ping: data=${e.data}`
|
||
|
document.body.append(p)
|
||
|
})
|
||
|
window.addEventListener('unload', () => {
|
||
|
es.close()
|
||
|
})
|
||
|
}
|
||
|
</script>
|
||
|
</head>
|
||
|
<body></body>
|
||
|
</html>
|