Refactor and cleanup code
This commit is contained in:
parent
5a40882dd7
commit
7d4953fea6
9 changed files with 7 additions and 54 deletions
|
@ -1,8 +1,8 @@
|
||||||
import React, { useContext, useEffect, useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
import Chat from "./Chat/Chat";
|
import Chat from "./Chat/Chat";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import { LangType, Message } from "./Chat/messageTypes";
|
import { LangType, Message } from "./type/messageTypes";
|
||||||
import { MessageDisplay } from "./Chat/MessageDisplay";
|
import { MessageDisplay } from "./MessageDisplay/MessageDisplay";
|
||||||
import strings from "./Intl/strings.json";
|
import strings from "./Intl/strings.json";
|
||||||
import { LangContext, LoginContext, LoginType } from "./context";
|
import { LangContext, LoginContext, LoginType } from "./context";
|
||||||
import { contentTypes, domain, endpoints, port } from "./consts";
|
import { contentTypes, domain, endpoints, port } from "./consts";
|
||||||
|
|
|
@ -5,9 +5,9 @@ import React, {
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { MessageDisplay } from "./MessageDisplay";
|
import { MessageDisplay } from "../MessageDisplay/MessageDisplay";
|
||||||
import { Client } from "@stomp/stompjs";
|
import { Client } from "@stomp/stompjs";
|
||||||
import { Message, MessageType } from "./messageTypes";
|
import { Message, MessageType } from "../type/messageTypes";
|
||||||
import "./Chat.css";
|
import "./Chat.css";
|
||||||
import strings from "../Intl/strings.json";
|
import strings from "../Intl/strings.json";
|
||||||
import { LangContext } from "../context";
|
import { LangContext } from "../context";
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { contentTypes, domain, endpoints, port } from "../consts";
|
import { contentTypes, domain, endpoints, port } from "../consts";
|
||||||
import { LoginType } from "../context";
|
import { LoginType } from "../context";
|
||||||
import { User } from "../Chat/userTypes";
|
import { User } from "../type/userTypes";
|
||||||
import "./Login.css";
|
import "./Login.css";
|
||||||
const encrypt = (rawPasswordString: string) => {
|
const encrypt = (rawPasswordString: string) => {
|
||||||
// TODO Encryption method stub
|
// TODO Encryption method stub
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useContext } from "react";
|
import React, { useContext } from "react";
|
||||||
import { Message, MessageType } from "./messageTypes";
|
import { Message, MessageType } from "../type/messageTypes";
|
||||||
import { LangContext } from "../context";
|
import { LangContext } from "../context";
|
||||||
import strings from "../Intl/strings.json";
|
import strings from "../Intl/strings.json";
|
||||||
import "./MessageDisplay.css";
|
import "./MessageDisplay.css";
|
|
@ -1,5 +1,5 @@
|
||||||
import { createContext } from "react";
|
import { createContext } from "react";
|
||||||
import { LangType } from "./Chat/messageTypes";
|
import { LangType } from "./type/messageTypes";
|
||||||
export type LoginType = {
|
export type LoginType = {
|
||||||
username: string;
|
username: string;
|
||||||
lastSeen: number;
|
lastSeen: number;
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
import { Client } from "@stomp/stompjs";
|
|
||||||
import { Message, MessageType } from "../Chat/messageTypes";
|
|
||||||
const domain = window.location.hostname;
|
|
||||||
const port = "8080";
|
|
||||||
const connectionAddress = `ws://${domain}:${port}/ws`;
|
|
||||||
const endpoints = {
|
|
||||||
destination: "/app/chat",
|
|
||||||
subscription: "/sub/chat",
|
|
||||||
history: "/api/v1/msg/",
|
|
||||||
};
|
|
||||||
export const createStompConnection = (
|
|
||||||
user: string,
|
|
||||||
subUpdateHandler: (message: Message) => void
|
|
||||||
) => {
|
|
||||||
const stompClient = new Client({
|
|
||||||
brokerURL: connectionAddress,
|
|
||||||
});
|
|
||||||
stompClient.onConnect = (frame) => {
|
|
||||||
stompClient.subscribe(endpoints.subscription, (message) => {
|
|
||||||
console.log(`Collected new message: ${message.body}`);
|
|
||||||
const messageBody = JSON.parse(message.body) as Message;
|
|
||||||
console.log(messageBody);
|
|
||||||
subUpdateHandler(messageBody);
|
|
||||||
});
|
|
||||||
stompClient.publish({
|
|
||||||
body: JSON.stringify({
|
|
||||||
type: MessageType.HELLO,
|
|
||||||
fromUserId: user,
|
|
||||||
toUserId: "everyone",
|
|
||||||
content: `${user} has joined the server!`,
|
|
||||||
timeMillis: Date.now(),
|
|
||||||
}),
|
|
||||||
destination: endpoints.destination,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generic error handlers
|
|
||||||
stompClient.onWebSocketError = (error) => {
|
|
||||||
console.error("Error with websocket", error);
|
|
||||||
};
|
|
||||||
|
|
||||||
stompClient.onStompError = (frame) => {
|
|
||||||
console.error("Broker reported error: " + frame.headers["message"]);
|
|
||||||
console.error("Additional details: " + frame.body);
|
|
||||||
};
|
|
||||||
return stompClient;
|
|
||||||
};
|
|
Loading…
Add table
Add a link
Reference in a new issue