Skip to content

Commit

Permalink
feat: ws protocol based on location protocol (#31)
Browse files Browse the repository at this point in the history
This changes fixes`Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.` when server is listen on 443 port.
  • Loading branch information
daliborgogic authored May 2, 2020
1 parent 93167d6 commit 9af9ec1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ console.log('[vite] connecting...')

declare var __VUE_HMR_RUNTIME__: HMRRuntime

const socket = new WebSocket(`ws://${location.host}`)
const socketProtocol = location.protocol === 'https:' ? 'wss' : 'ws'
const socket = new WebSocket(`${socketProtocol}://${location.host}`)

// Listen for messages
socket.addEventListener('message', ({ data }) => {
Expand Down Expand Up @@ -64,7 +65,7 @@ socket.addEventListener('message', ({ data }) => {
socket.addEventListener('close', () => {
console.log(`[vite] server connection lost. polling for restart...`)
setInterval(() => {
new WebSocket(`ws://${location.host}`).addEventListener('open', () => {
new WebSocket(`${socketProtocol}://${location.host}`).addEventListener('open', () => {
location.reload()
})
}, 1000)
Expand Down

0 comments on commit 9af9ec1

Please sign in to comment.