Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
}
},
mounted() {
this.$websocket();
this.$websocket.init();
},
};
</script>
Expand Down
13 changes: 11 additions & 2 deletions plugins/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Context } from '@nuxt/types';

import { RepositoryMessage } from './notifications';

let ws: WebSocket;

function initConnection(context: Context) {
const { auth } = context.store.state;

Expand All @@ -11,6 +13,10 @@ function initConnection(context: Context) {
}
}

function abortConnection() {
ws.close();
}

const connectToWebSocket = (context: Context): void => {
const {
app,
Expand All @@ -20,7 +26,7 @@ const connectToWebSocket = (context: Context): void => {
},
} = context;

const ws = new WebSocket(
ws = new WebSocket(
`wss://staging.openwiden.com/websocket/?access_token=${accessToken}`
);

Expand All @@ -47,5 +53,8 @@ const connectToWebSocket = (context: Context): void => {
};

export default (context: Context, inject: any) => {
inject('websocket', () => initConnection(context));
inject('websocket', {
init: () => initConnection(context),
close: abortConnection,
});
};
6 changes: 3 additions & 3 deletions store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export const actions: any = {
},

logoutUser({ commit }: any) {
this.$websocket.close();

removeCookies(['auth', 'refresh', 'provider']);
commit(MUTATIONS.RESET_AUTH);

this.$websocket();
},

loginUser({ commit }: any, { provider }: { provider: string }) {
Expand All @@ -90,7 +90,7 @@ export const actions: any = {
commit(MUTATIONS.SET_AUTH, authToken);
commit(MUTATIONS.SET_REFRESH, refreshToken);

this.$websocket();
this.$websocket.init();
},

async getUser(
Expand Down