Skip to content

Instantly share code, notes, and snippets.

@scambier
Last active April 5, 2019 11:46
Show Gist options
  • Select an option

  • Save scambier/fe11206c8daa1afa549ed80969f5f96c to your computer and use it in GitHub Desktop.

Select an option

Save scambier/fe11206c8daa1afa549ed80969f5f96c to your computer and use it in GitHub Desktop.
VueJS service for SignalR
import { HubConnectionBuilder, LogLevel } from '@aspnet/signalr'
async function wait(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
const hubUrl = 'https://localhost:44360/testHub'
const connection = new HubConnectionBuilder()
.withUrl(hubUrl)
.configureLogging(LogLevel.Information)
.build()
async function start() {
let connected = false
let timeout = 0
while (!connected) {
try {
console.log('Connecting to SignalR...')
await connection.start()
connected = true
console.log('Connected to SignalR.')
} catch (e) {
console.warn(`Failed to connect to SignalR, will retry in ${++timeout} second(s)`)
console.error(e)
}
await wait(timeout * 1000)
}
}
connection.on('ReceiveMessage', (...params) => {
console.log(params)
})
connection.onclose(() => {
console.log('Disconnected from SignalR')
start()
})
start()
export default connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment