Current location - Quotes Website - Personality signature - Global encapsulation of SignalR in vue
Global encapsulation of SignalR in vue
I won't say more about what it is. I can go to official website to check it myself.

1. install signalr without installing jquery and other things.

2. Create a new file named signalR.js

3. Introduce main.js and mount it globally.

initialization

My requirement is to connect to the server after logging in, so I put the initial connection step (initSR) at the time when I successfully log in and get the token.

But at the same time, I found that when the interface is refreshed, the signaR mounted globally in main.js will disappear, so it needs to be reinitialized in App.vue

use

My demand is the theme of getNotification at the Subscriber. When the server-side task is completed, it will send me a message. I got this message and did the following work according to the content of the message.

Special note: When the life cycle of this component ends, remember to destroy the registered method, otherwise the above callback will be triggered repeatedly (I tried the off method used by official website, which was ineffective, so I manually cleared the array myself, and there is a better way to inform me).