Last active
April 5, 2019 11:46
-
-
Save scambier/fe11206c8daa1afa549ed80969f5f96c to your computer and use it in GitHub Desktop.
VueJS service for SignalR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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