mirror of
https://github.com/wassname/talk.git
synced 2026-08-13 12:40:11 +08:00
Merge branch 'master' into port-perm-checks
This commit is contained in:
@@ -4,6 +4,7 @@ import * as actions from '../constants/auth';
|
||||
import * as Storage from '../helpers/storage';
|
||||
import coralApi, {base} from '../helpers/request';
|
||||
|
||||
import {resetWebsocket} from 'coral-framework/services/client';
|
||||
import t from 'coral-framework/services/i18n';
|
||||
|
||||
export const showSignInDialog = () => (dispatch, getState) => {
|
||||
@@ -117,6 +118,7 @@ const signInFailure = (error) => ({
|
||||
export const handleAuthToken = (token) => (dispatch) => {
|
||||
Storage.setItem('exp', jwtDecode(token).exp);
|
||||
Storage.setItem('token', token);
|
||||
|
||||
dispatch({type: 'HANDLE_AUTH_TOKEN'});
|
||||
};
|
||||
|
||||
@@ -277,6 +279,10 @@ export const logout = () => (dispatch) => {
|
||||
if (!bowser.safari && !bowser.ios) {
|
||||
Storage.removeItem('token');
|
||||
}
|
||||
|
||||
// Reset the websocket.
|
||||
resetWebsocket();
|
||||
|
||||
dispatch({type: actions.LOGOUT});
|
||||
});
|
||||
};
|
||||
@@ -307,6 +313,9 @@ export const checkLogin = () => (dispatch) => {
|
||||
|
||||
dispatch(checkLoginSuccess(result.user));
|
||||
|
||||
// Reset the websocket.
|
||||
resetWebsocket();
|
||||
|
||||
// Display create username dialog if necessary.
|
||||
if (result.user.canEditName && result.user.status !== 'BANNED') {
|
||||
dispatch(showCreateUsernameDialog());
|
||||
|
||||
@@ -1,8 +1,31 @@
|
||||
import ApolloClient, {addTypename} from 'apollo-client';
|
||||
import {networkInterface} from './transport';
|
||||
import {SubscriptionClient, addGraphQLSubscriptions} from 'subscriptions-transport-ws';
|
||||
import {SUBSCRIPTION_END} from 'subscriptions-transport-ws/dist/messageTypes';
|
||||
import {getAuthToken} from '../helpers/request';
|
||||
|
||||
let client;
|
||||
let client, wsClient = null, wsClientToken = null;
|
||||
|
||||
export function resetWebsocket() {
|
||||
if (wsClient === null) {
|
||||
|
||||
// Nothing to reset!
|
||||
return;
|
||||
}
|
||||
|
||||
// Unsubscribe from all the active subscriptions.
|
||||
Object.keys(wsClient.subscriptions).forEach((id) => {
|
||||
|
||||
// Create the message.
|
||||
let message = {id: parseInt(id), type: SUBSCRIPTION_END};
|
||||
|
||||
// Send the unsubscribe message.
|
||||
wsClient.client.send(JSON.stringify(message));
|
||||
});
|
||||
|
||||
// Close the client, this will trigger a reconnect.
|
||||
wsClient.close();
|
||||
}
|
||||
|
||||
export function getClient() {
|
||||
if (client) {
|
||||
@@ -10,8 +33,21 @@ export function getClient() {
|
||||
}
|
||||
|
||||
const protocol = location.protocol === 'https:' ? 'wss' : 'ws';
|
||||
const wsClient = new SubscriptionClient(`${protocol}://${location.host}/api/v1/live`, {
|
||||
reconnect: true
|
||||
wsClient = new SubscriptionClient(`${protocol}://${location.host}/api/v1/live`, {
|
||||
reconnect: true,
|
||||
connectionParams: {
|
||||
get token() {
|
||||
|
||||
wsClientToken = getAuthToken();
|
||||
|
||||
// Try to get the token from localStorage. If it isn't here, it may
|
||||
// be passed as a cookie.
|
||||
|
||||
// NOTE: THIS IS ONLY EVER EVALUATED ONCE, IN ORDER TO SEND A DIFFERNT
|
||||
// TOKEN YOU MUST DISCONNECT AND RECONNECT THE WEBSOCKET CLIENT.
|
||||
return wsClientToken;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
|
||||
|
||||
Reference in New Issue
Block a user