Skip to content

Commit

Permalink
added the anony chat feat basic
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmay dhobale authored and tanmay dhobale committed Jun 6, 2024
1 parent 28a77f4 commit 731991a
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 612 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@types/lunr": "^2.3.7",
"@types/remove-markdown": "^0.3.4",
"@types/ua-parser-js": "^0.7.36",
"@types/uuid": "^9.0.8",
"autoprefixer": "^10.4.19",
"gray-matter": "^4.0.3",
"husky": ">=6",
Expand Down
52 changes: 48 additions & 4 deletions pages/Chatnow.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { v4 as uuidv4 } from "uuid";
import {
Box,
Container,
Expand All @@ -13,6 +14,7 @@ import {
import Head from "next/head";
import Image from "next/image";
import { NextPage } from "next";
import chatBackground from "@/public/assets/heroImages/chatBackground.svg"; // Adjust according to your actual path

interface Message {
text: string;
Expand All @@ -24,24 +26,63 @@ const ChatNow: NextPage = () => {
const [inChat, setInChat] = useState<boolean>(false);
const [messages, setMessages] = useState<Message[]>([]);
const [message, setMessage] = useState<string>("");
const [ws, setWs] = useState<WebSocket | null>(null);
const [matchId, setMatchId] = useState<string | null>(null);

useEffect(() => {
if (inChat && !ws) {
const websocket = new WebSocket("ws://localhost:3001"); // Adjust to your server address
const userId = uuidv4();
websocket.onopen = () => {
websocket.send(
JSON.stringify({ type: "register", id: userId, interest })
);
};
websocket.onmessage = (event) => {
const data = JSON.parse(event.data);

if (data.type === "match") {
setMatchId(data.id);
} else if (data.type === "message") {
const receivedMessage: Message = { text: data.text, from: "system" };
setMessages((prevMessages) => [...prevMessages, receivedMessage]);
} else if (data.type === "system") {
const systemMessage: Message = { text: data.text, from: "system" };
setMessages((prevMessages) => [...prevMessages, systemMessage]);
}
};
websocket.onclose = () =>
console.log("Disconnected from WebSocket server");
setWs(websocket);
}
}, [inChat, ws, interest]);

const handleStartChat = () => {
setInChat(true);
setMessages([
{
text: "Hello! What would you like to discuss about " + interest + "?",
text: "Looking for someone to chat about " + interest + "...",
from: "system",
},
]);
};

const handleMessageSend = () => {
if (message.trim() !== "") {
setMessages([...messages, { text: message, from: "user" }]);
if (message.trim() !== "" && matchId) {
const userMessage: Message = { text: message, from: "user" };
setMessages([...messages, userMessage]);
ws?.send(JSON.stringify({ type: "message", to: matchId, text: message }));
setMessage(""); // Clear input after send
}
};

const handleEndChat = () => {
if (ws) {
ws.send(JSON.stringify({ type: "end" }));
setMessages([{ text: "Looking for a new match...", from: "system" }]);
}
};

const bgUser = useColorModeValue("blue.100", "blue.900");
const bgSystem = useColorModeValue("gray.200", "gray.700");

Expand Down Expand Up @@ -124,6 +165,9 @@ const ChatNow: NextPage = () => {
<Button onClick={handleMessageSend} colorScheme="blue">
Send
</Button>
<Button onClick={handleEndChat} colorScheme="red">
End Chat
</Button>
</HStack>
</VStack>
)}
Expand Down
Loading

0 comments on commit 731991a

Please sign in to comment.