]> Repos - mime-chat/commitdiff
close session on logout
authorEnrique Hernandez <hernandeze2@xavier.edu>
Thu, 3 Jun 2021 13:55:50 +0000 (09:55 -0400)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Thu, 3 Jun 2021 13:55:50 +0000 (09:55 -0400)
src/background/ipc/account.ts
src/background/session.ts
src/background/websockets.ts
tests/server.js

index c9594be1647b432d5548da92963387b7987881f6..589850918e77e263388a4a566082eb89286cf389 100644 (file)
@@ -5,6 +5,7 @@ import { Profile } from "@/types";
 import { submit, fetchProfile, logout } from "@/api/account";
 import { ipcMain, IpcMainInvokeEvent } from "electron";
 import { store } from "@/background/store";
+import { endSession } from "@/background/session";
 
 
 const parseAuthRes = (authRes: any) => {
@@ -94,6 +95,7 @@ const onLogout = async (
             store.delete('crimataId');
 
             // TODO: kill crimata platform session
+            endSession();
 
             resolve();
         } catch(e) {
index fac657219a3614c43ca8e3a0bf85016e3cb5f785..ebfbc265396dfd6aee791c5e940c7f78ce702920 100644 (file)
@@ -10,6 +10,7 @@
 
 import { backgroundMitt } from "@/modules/emitter";
 import { ipcEmit, loadState } from './helpers';
+import WebSocket from 'ws';
 
 import useWebSockets from "./websockets";
 
@@ -22,6 +23,8 @@ let win = true;
 // Info saved to json on quit (key, newMessages).
 let state: SessionState;
 
+let socket: WebSocket | null = null;
+
 
 // Calls appropriate endpoint for a server message.
 const onMessage = (data: string): void => {
@@ -88,6 +91,13 @@ export const sendMessage = (payload: Record<string, any>): void => {
 
 }
 
+export const endSession = (): void => {
+    if (socket) {
+        socket.close();
+        socket = null;
+    }
+}
+
 
 // Call this to initialize session with Crimata servers.
 export const initSession = (authPayload: {
@@ -100,7 +110,8 @@ export const initSession = (authPayload: {
     state = loadState("session.json");
 
     // Open socket connection.
-    createSocket(authPayload);
+    if (!socket)
+    socket = createSocket(authPayload);
 
     // Keep win up-to-date.
     backgroundMitt.on('window-active', (state: boolean) => {
index 5556ece17d46eb711f5562624c8f568a7932a1bd..36eae6f6df3ec4de35c6a6cd6a64063dcfb5f8eb 100644 (file)
@@ -1,3 +1,4 @@
+
 "use strict";
 
 import WebSocket from 'ws';
@@ -68,7 +69,7 @@ export default function useWebSockets(
     const createSocket = (authPayload: {
         token: string;
         crimataId: string;
-    }) => {
+    }):WebSocket => {
         socket = new WebSocket(socketUrl)
 
         // Add listeners.
@@ -82,7 +83,8 @@ export default function useWebSockets(
         socket.addEventListener("close", onClose)
         socket.addEventListener("error", onError)
 
-        console.log("WS:New socket created.")
+        return socket;
+
     }
 
     return {
index 6731be96178d94f479a9eb5a2d5c163a381b26b7..9616e8a8ab1cde05bc84d3f8eb45e4515e81286d 100644 (file)
@@ -6,6 +6,7 @@ const wss = new WebSocket.Server({
 let auth = false;
 
 wss.on("connection", function connection(ws, req) {
+
   ws.on("message", function incoming(message) {
 
     console.log(message)
@@ -19,13 +20,13 @@ wss.on("connection", function connection(ws, req) {
         auth = true;
       } else {
         const parsed = JSON.parse(message);
-        console.log('got a message', message)
+        console.log('got a message', message);
 
       }
 
     } else {
       const parsed = JSON.parse(message);
-      console.log('parsed', parsed)
+      console.log('parsed', parsed);
     }
 
   });