mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 09:27:08 +00:00
feat: improves examples
This commit is contained in:
parent
98257b470a
commit
c93d379f20
@ -1,32 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>go-wasm-http-server hello demo</title>
|
||||
<title>go-wasm-http-server hello sse demo</title>
|
||||
<script>
|
||||
navigator.serviceWorker.register('sw.js')
|
||||
.then(registration => {
|
||||
const serviceWorker = registration.installing ?? registration.waiting ?? registration.active
|
||||
if (serviceWorker.state === 'activated') {
|
||||
startEventSource()
|
||||
document.querySelector('#open-button').disabled = false
|
||||
} else {
|
||||
serviceWorker.addEventListener('statechange', e => {
|
||||
if (e.target.state === 'activated') startEventSource()
|
||||
if (e.target.state === 'activated') {
|
||||
document.querySelector('#open-button').disabled = false
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
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('DOMContentLoaded', () => {
|
||||
let es;
|
||||
|
||||
document.querySelector('#open-button').addEventListener('click', () => {
|
||||
if (es && es.readyState === es.OPEN) return
|
||||
es = new EventSource('api/events')
|
||||
es.addEventListener('ping', (e) => {
|
||||
const li = document.createElement('li')
|
||||
li.textContent = `ping: data=${e.data}`
|
||||
document.querySelector('ul').append(li)
|
||||
})
|
||||
})
|
||||
|
||||
document.querySelector('#close-button').addEventListener('click', () => {
|
||||
if (!es) return
|
||||
es.close()
|
||||
})
|
||||
|
||||
document.querySelector('#clear-button').addEventListener('click', () => {
|
||||
document.querySelector('ul').innerHTML = ''
|
||||
})
|
||||
|
||||
window.addEventListener('unload', () => {
|
||||
if (!es) return
|
||||
es.close()
|
||||
})
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</head>
|
||||
<body></body>
|
||||
<body>
|
||||
<p>
|
||||
<button id="open-button" disabled>Open event source</button>
|
||||
<button id="close-button">Close event source</button>
|
||||
<button id="clear-button">Clear events</button>
|
||||
</p>
|
||||
<ul></ul>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,12 +1,14 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
|
||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||
|
||||
const wasm = 'api.wasm'
|
||||
|
||||
addEventListener('install', (event) => {
|
||||
event.waitUntil(skipWaiting())
|
||||
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
|
||||
})
|
||||
|
||||
addEventListener('activate', event => {
|
||||
addEventListener('activate', (event) => {
|
||||
event.waitUntil(clients.claim())
|
||||
})
|
||||
|
||||
registerWasmHTTPListener('api.wasm', { base: 'api' })
|
||||
registerWasmHTTPListener(wasm, { base: 'api' })
|
||||
|
@ -1,8 +1,10 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
|
||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||
importScripts('../sw.js')
|
||||
|
||||
const wasm = '../hello-state/api.wasm'
|
||||
|
||||
addEventListener('install', event => {
|
||||
event.waitUntil(caches.open('hello-state').then((cache) => cache.add('api.wasm')))
|
||||
event.waitUntil(caches.open('hello-state').then((cache) => cache.add(wasm)))
|
||||
})
|
||||
|
||||
addEventListener('activate', event => {
|
||||
@ -11,4 +13,4 @@ addEventListener('activate', event => {
|
||||
|
||||
addEventListener('message', () => {})
|
||||
|
||||
registerWasmHTTPListener('../hello-state/api.wasm', { base: 'api' })
|
||||
registerWasmHTTPListener(wasm, { base: 'api' })
|
||||
|
@ -1,13 +1,14 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
|
||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||
|
||||
const wasm = 'api.wasm'
|
||||
|
||||
addEventListener('install', (event) => {
|
||||
event.waitUntil(caches.open('hello-state').then((cache) => cache.add('api.wasm')))
|
||||
event.waitUntil(skipWaiting())
|
||||
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
|
||||
})
|
||||
|
||||
addEventListener('activate', (event) => {
|
||||
event.waitUntil(clients.claim())
|
||||
})
|
||||
|
||||
registerWasmHTTPListener('api.wasm', { base: 'api' })
|
||||
registerWasmHTTPListener(wasm, { base: 'api' })
|
||||
|
@ -1,12 +1,14 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
|
||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||
|
||||
const wasm = 'api.wasm'
|
||||
|
||||
addEventListener('install', (event) => {
|
||||
event.waitUntil(caches.open('hello').then((cache) => cache.add('api.wasm')))
|
||||
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
|
||||
})
|
||||
|
||||
addEventListener('activate', (event) => {
|
||||
event.waitUntil(clients.claim())
|
||||
})
|
||||
|
||||
registerWasmHTTPListener('api.wasm', { base: 'api' })
|
||||
registerWasmHTTPListener(wasm, { base: 'api' })
|
||||
|
Loading…
x
Reference in New Issue
Block a user