diff --git a/core/chat/server.go b/core/chat/server.go index 30cd4b995..19d071b05 100644 --- a/core/chat/server.go +++ b/core/chat/server.go @@ -183,7 +183,12 @@ func (s *Server) HandleClientConnection(w http.ResponseWriter, r *http.Request) _, _ = w.Write([]byte(events.ErrorMaxConnectionsExceeded)) return } - upgrader.CheckOrigin = func(r *http.Request) bool { return true } + + // To allow dev web environments to connect. + upgrader.CheckOrigin = func(r *http.Request) bool { + return true + } + conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Debugln(err) diff --git a/web/components/stores/ClientConfigStore.tsx b/web/components/stores/ClientConfigStore.tsx index 80a626595..0cf873ff7 100644 --- a/web/components/stores/ClientConfigStore.tsx +++ b/web/components/stores/ClientConfigStore.tsx @@ -63,7 +63,7 @@ export function ClientConfigStore() { const setClientConfig = useSetRecoilState(clientConfigStateAtom); const setChatVisibility = useSetRecoilState(chatVisibilityAtom); const setChatState = useSetRecoilState(chatStateAtom); - const setChatMessages = useSetRecoilState(chatMessagesAtom); + const [chatMessages, setChatMessages] = useRecoilState(chatMessagesAtom); const setChatDisplayName = useSetRecoilState(chatDisplayNameAtom); const [appState, setAppState] = useRecoilState(appStateAtom); const [accessToken, setAccessToken] = useRecoilState(accessTokenAtom); @@ -106,7 +106,7 @@ export function ClientConfigStore() { handleConnectedClientInfoMessage(message as ConnectedClientInfoEvent); break; case SocketMessageType.CHAT: - handleChatMessage(message as ChatEvent); + handleChatMessage(message as ChatEvent, chatMessages, setChatMessages); break; default: console.error('Unknown socket message type: ', message.type); @@ -116,8 +116,8 @@ export function ClientConfigStore() { const getChatHistory = async () => { try { const messages = await ChatService.getChatHistory(accessToken); - // console.log(`ChatService -> getChatHistory() messages: \n${JSON.stringify(messages)}`); - setChatMessages(messages); + const updatedChatMessages = [...messages, ...chatMessages]; + setChatMessages(updatedChatMessages); } catch (error) { console.error(`ChatService -> getChatHistory() ERROR: \n${error}`); } diff --git a/web/components/stores/eventhandlers/handleChatMessage.ts b/web/components/stores/eventhandlers/handleChatMessage.ts index 8a1e551d2..ea9e3512c 100644 --- a/web/components/stores/eventhandlers/handleChatMessage.ts +++ b/web/components/stores/eventhandlers/handleChatMessage.ts @@ -1,8 +1,11 @@ -import { - ChatEvent, - SocketEvent, -} from '../../../interfaces/socket-events'; +import { ChatMessage } from '../../../interfaces/chat-message.model'; +import { ChatEvent } from '../../../interfaces/socket-events'; -export default function handleChatMessage(message: ChatEvent) { - console.log('chat message', message); +export default function handleChatMessage( + message: ChatEvent, + messages: ChatMessage[], + setChatMessages, +) { + const updatedMessages = [...messages, message]; + setChatMessages(updatedMessages); } diff --git a/web/interfaces/socket-events.ts b/web/interfaces/socket-events.ts index 294a08082..fde34d67e 100644 --- a/web/interfaces/socket-events.ts +++ b/web/interfaces/socket-events.ts @@ -30,4 +30,4 @@ export interface ConnectedClientInfoEvent extends SocketEvent { export interface ChatEvent extends SocketEvent { user: User; body: string; -} \ No newline at end of file +} diff --git a/web/services/websocket-service.ts b/web/services/websocket-service.ts index fd58a815b..b82bebeb2 100644 --- a/web/services/websocket-service.ts +++ b/web/services/websocket-service.ts @@ -1,8 +1,6 @@ import { message } from 'antd'; import { SocketMessageType } from '../interfaces/socket-events'; - - interface SocketMessage { type: SocketMessageType; data: any; @@ -61,7 +59,7 @@ export default class WebsocketService { // On ws error just close the socket and let it re-connect again for now. onError(e) { - console.error(e) + console.error(e); handleNetworkingError(`Socket error: ${e}`); this.websocket.close(); // if (!this.isShutdown) {