Last active
July 31, 2019 16:06
-
-
Save Noitidart/57874fcbb22931b9543e286acf974b47 to your computer and use it in GitHub Desktop.
This file contains 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 io from 'socket.io-client'; | |
import getBaseUrl from '../../../utils/get-base-url'; | |
let mSocket: null | SocketIOClient.Socket = null; | |
let mIsConnecting = false; | |
/** | |
* Create io.socket if it doesn't exist. If it does, and it's disconnected | |
* then reconnect it. If io.socket is in process of connecting, this will | |
* resolve when the connection establishes. | |
* | |
* @param {string} [initialSIDCookie] | |
* | |
* @returns {Promise<void>} | |
*/ | |
export function connectSocket(initialSIDCookie?: string): Promise<void> { | |
return new Promise((resolve, reject) => { | |
if (!mSocket) { | |
mIsConnecting = true; | |
mSocket = io(getBaseUrl(), { | |
forceNew: false, | |
transports: ["websocket"], | |
reconnection: false, | |
query: { | |
nosession: 'true', | |
__sails_io_sdk_version: '1.2.1', | |
__sails_io_sdk_platform: 'node', | |
__sails_io_sdk_language: 'javascript' | |
}, | |
// query: 'nosession=true&__sails_io_sdk_version=1.2.1&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript', | |
...(!initialSIDCookie ? {} : { | |
extraHeaders: { | |
cookie: initialSIDCookie | |
} | |
}) | |
}); | |
} else if (isSocketConnected()) { | |
resolve(); | |
return; | |
} else if (!isSocketConnecting()) { | |
mSocket!.connect(); | |
} | |
// else is connecting | |
function handleError() { | |
mIsConnecting = false; | |
removeHandlers(); | |
reject(); | |
} | |
function handleConnect() { | |
mIsConnecting = false; | |
removeHandlers(); | |
resolve(); | |
} | |
function removeHandlers() { | |
mSocket!.off('error', handleError); | |
mSocket!.off('connect', handleConnect); | |
} | |
mSocket.on('error', handleError); | |
mSocket.on('connect', handleConnect); | |
}); | |
} | |
/** | |
* If socket is not connected this does nothing. If it | |
* is connected, this will disconnect it and resolve | |
* once disconnection has completed. | |
* | |
* @returns Promise<void> | |
*/ | |
export async function disconnectSocket() { | |
if (!mSocket) { | |
return; | |
} else if (isSocketConnecting()) { | |
try { | |
await connectSocket(); | |
} catch(ignore) { | |
return; | |
} | |
} else if (!isSocketConnected()) { | |
return; | |
} | |
return new Promise(resolve => { | |
mSocket!.on('disconnect', function resolver() { | |
mSocket!.off('disconnect', resolver); | |
resolve(); | |
}); | |
mSocket!.disconnect(); | |
}); | |
} | |
export function isSocketConnecting() { | |
return mIsConnecting; | |
} | |
export function getSocket() { | |
return mSocket; | |
} | |
export function isSocketConnected() { | |
return !mSocket ? false : mSocket.connected; | |
} | |
export function emitToSailsSocket( | |
url: string, | |
{ | |
method = 'get', | |
data, | |
headers | |
}: { | |
method?: 'get' | 'post' | 'put' | 'patch' | 'delete', | |
data?: object, | |
headers?: object | |
} = {} | |
): Promise<{ | |
statusCode: number, | |
body: object, | |
headers?: object | |
}> { | |
return new Promise((resolve, reject) => { | |
if (!mSocket) { | |
reject('socket not initialized'); | |
} else if (!isSocketConnected()) { | |
reject('socket is not connected'); | |
} else { | |
mSocket!.emit(method, { url, data, headers }, resolve) | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment