add socket connect and reconnect to session code
This commit is contained in:
parent
e0da50eb27
commit
b05cb202b6
9 changed files with 140 additions and 166 deletions
|
|
@ -2,77 +2,84 @@ import WebSocket from 'ws';
|
|||
import { windowEmitter } from './windowEmitter';
|
||||
import { ipcMain } from "electron";
|
||||
|
||||
type Message = {
|
||||
type: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface RenderMessage extends Message {
|
||||
interface RenderMessage {
|
||||
type: 'render';
|
||||
content: string;
|
||||
context: string;
|
||||
subContext: string;
|
||||
modifiers: string;
|
||||
time: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface AuthUser extends Message {
|
||||
interface UserCreds {
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
access_token: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const ip = 'ws://localhost';
|
||||
const port = 8080;
|
||||
const port = 8081;
|
||||
const reconnectTimeout = 3000; //ms
|
||||
let socket: WebSocket;
|
||||
let success = false;
|
||||
let auth = false;
|
||||
|
||||
const receiveMessage = (message: string): void => {
|
||||
// NOTE assuming message is JSON string
|
||||
if (message === 'locked') {
|
||||
windowEmitter.emit('auth-resp', message);
|
||||
return;
|
||||
}
|
||||
const parsed: RenderMessage | AuthUser = JSON.parse(message);
|
||||
if (parsed.type === "render") {
|
||||
windowEmitter.emit('ipc-renderer', {
|
||||
endpoint: 'render-message',
|
||||
message: parsed
|
||||
});
|
||||
}
|
||||
|
||||
while(!auth) {
|
||||
windowEmitter.emit('auth-res', message);
|
||||
return;
|
||||
}
|
||||
|
||||
const parsed: RenderMessage = JSON.parse(message);
|
||||
if (parsed.type === "render") {
|
||||
windowEmitter.emit('ipc-renderer', {
|
||||
endpoint: 'render-message',
|
||||
message: parsed
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const authToken = async (event: any, token: string): Promise<string | AuthUser > => {
|
||||
const authUser = async (event: any, payload: string | UserCreds | null): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log('authenticating...');
|
||||
windowEmitter.on('auth-resp', (arg: string | AuthUser) => {
|
||||
if (arg === 'locked') {
|
||||
reject(arg);
|
||||
windowEmitter.on('auth-res', (res: string) => {
|
||||
if (res === 'locked') {
|
||||
reject(res);
|
||||
} else {
|
||||
auth = true;
|
||||
resolve(res);
|
||||
}
|
||||
resolve(arg);
|
||||
});
|
||||
if (token) {
|
||||
socket.send(token);
|
||||
} else {
|
||||
if (!payload) {
|
||||
socket.send('no-token');
|
||||
} else if (typeof payload === 'string' || payload instanceof String) {
|
||||
socket.send(payload);
|
||||
} else {
|
||||
socket.send(JSON.stringify(payload));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Connect and authenticate
|
||||
export function initSession() {
|
||||
export const initSession = () => {
|
||||
if (socket) {
|
||||
socket.removeAllListeners();
|
||||
socket.terminate();
|
||||
socket.close();
|
||||
success = false;
|
||||
}
|
||||
|
||||
console.log('Connecting to Crimata Servers...');
|
||||
|
||||
socket = new WebSocket(`${ip}:${port}`);
|
||||
socket.binaryType = 'arraybuffer';
|
||||
|
||||
socket.on('open', () => {
|
||||
console.log('Connected to crimata-platform');
|
||||
success = true;
|
||||
console.log('Success! Connected to Crimata.');
|
||||
|
||||
// handle renderer auth-token event
|
||||
ipcMain.removeHandler('auth-token'); // avoid setting duplicate handlers
|
||||
ipcMain.handle('auth-token', authToken);
|
||||
ipcMain.removeHandler('auth-user'); // avoid setting duplicate handlers
|
||||
ipcMain.handle('auth-user', authUser);
|
||||
|
||||
// fetch token from renderer
|
||||
windowEmitter.emit('ipc-renderer', {
|
||||
|
|
@ -80,28 +87,28 @@ export function initSession() {
|
|||
message: null
|
||||
});
|
||||
|
||||
success = true;
|
||||
});
|
||||
|
||||
socket.on('error', (e) => {
|
||||
console.log('ERROR: Failed to connect.');
|
||||
socket.removeAllListeners();
|
||||
socket.close();
|
||||
setTimeout(()=> {
|
||||
if (!success) {
|
||||
console.log('Attempting reconnect.');
|
||||
initSession();
|
||||
}
|
||||
}, reconnectTimeout);
|
||||
|
||||
})
|
||||
|
||||
socket.on('close', () => {
|
||||
console.log('Connection droped. Restarting.')
|
||||
initSession();
|
||||
})
|
||||
|
||||
socket.on("message", receiveMessage);
|
||||
|
||||
setTimeout(()=> {
|
||||
if (!success) {
|
||||
// initSession();
|
||||
throw new Error('Unable to connect to crimata-platform');
|
||||
}
|
||||
}, reconnectTimeout);
|
||||
}
|
||||
|
||||
export const authLogin = (event: any, arg: any) => {
|
||||
try {
|
||||
sendMessage(arg)
|
||||
windowEmitter.on('auth-resp', (payload: AuthUser) => {
|
||||
event.reply('auth-login-reply', payload);
|
||||
})
|
||||
} catch(e) {
|
||||
console.log('Error loging in' + e);
|
||||
event.reply('auth-login-reply', e);
|
||||
}
|
||||
}
|
||||
|
||||
export const sendMessage = (content: string | Buffer) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue