34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
// 连接信令服务器
|
|
const protocol = window.location.protocol.replace('http', 'ws')
|
|
const ws = new WebSocket(`${protocol}//${window.location.host}/api`)
|
|
ws.onopen = () => {
|
|
console.log('WebSocket connection opened')
|
|
ws.send(JSON.stringify({ type: 'init', data: 'Hello Server' }))
|
|
}
|
|
ws.onmessage = (event) => {
|
|
console.log('Message from server:', event.data)
|
|
}
|
|
ws.onerror = (error) => {
|
|
console.error('WebSocket error:', error)
|
|
}
|
|
ws.onclose = () => {
|
|
console.log('WebSocket connection closed')
|
|
}
|
|
|
|
// Service Worker
|
|
if ('serviceWorker' in navigator) {
|
|
navigator.serviceWorker.addEventListener('message', event => {
|
|
console.log('收到消息 Service Worker: ', event.data) // 接收消息
|
|
event.source.postMessage('Hello from main thread') // 回应 Service Worker
|
|
})
|
|
navigator.serviceWorker.register('/sw.js')
|
|
//.then((registration) => {
|
|
// console.log('Service Worker 注册成功')
|
|
// registration.showNotification('Hello World')
|
|
//})
|
|
}
|
|
|
|
//navigator.storage.estimate().then((estimate) => {
|
|
// console.log('Storage usage: ', estimate.usage / 1000 / 1000 / 1000, estimate.quota / 1000 / 1000 / 1000)
|
|
//})
|