fix stuff

This commit is contained in:
Jeff Emmett 2024-08-31 15:00:06 +02:00
parent a8c8d62e63
commit 2c35a0c53c
4 changed files with 14 additions and 14 deletions

View File

@ -10,7 +10,7 @@ import {
import { useParams } from 'react-router-dom' // Add this import import { useParams } from 'react-router-dom' // Add this import
import { ChatBoxTool } from '@/tools/ChatBoxTool' import { ChatBoxTool } from '@/tools/ChatBoxTool'
import { IChatBoxShape, ChatBoxShape } from '@/shapes/ChatBoxShape' import { IChatBoxShape, ChatBoxShape } from '@/shapes/ChatBoxShape'
import { multiplayerAssetStore } from '@/client/multiplayerAssetStore' import { multiplayerAssetStore } from '../client/multiplayerAssetStore' // Adjusted path if necessary
import { customSchema } from '../../worker/TldrawDurableObject' import { customSchema } from '../../worker/TldrawDurableObject'
const WORKER_URL = `https://jeffemmett-canvas.jeffemmett.workers.dev` const WORKER_URL = `https://jeffemmett-canvas.jeffemmett.workers.dev`

View File

@ -40,7 +40,7 @@ interface Message {
} }
// Add this new component after the ChatBoxShape class // Add this new component after the ChatBoxShape class
function ChatBox({ width, height }: { roomId: string, width: number, height: number }) { function ChatBox({ roomId, width, height }: { roomId: string, width: number, height: number }) {
const [messages, setMessages] = useState<Message[]>([]); const [messages, setMessages] = useState<Message[]>([]);
const [inputMessage, setInputMessage] = useState(""); const [inputMessage, setInputMessage] = useState("");
const [username, setUsername] = useState("jeff"); const [username, setUsername] = useState("jeff");
@ -55,12 +55,11 @@ function ChatBox({ width, height }: { roomId: string, width: number, height: num
setUsername(newUsername); setUsername(newUsername);
localStorage.setItem("chatUsername", newUsername); localStorage.setItem("chatUsername", newUsername);
} }
fetchMessages(roomId);
fetchMessages(); const interval = setInterval(() => fetchMessages(roomId), 2000);
const interval = setInterval(fetchMessages, 2000);
return () => clearInterval(interval); return () => clearInterval(interval);
}, []); }, [roomId]);
useEffect(() => { useEffect(() => {
if (messagesEndRef.current) { if (messagesEndRef.current) {
@ -68,9 +67,9 @@ function ChatBox({ width, height }: { roomId: string, width: number, height: num
} }
}, [messages]); }, [messages]);
const fetchMessages = async () => { const fetchMessages = async (roomId: string) => {
try { try {
const response = await fetch("https://jeffemmett-realtimechatappwithpolling.web.val.run?action=getMessages"); const response = await fetch(`https://jeffemmett-realtimechatappwithpolling.web.val.run?action=getMessages&roomId=${roomId}`);
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
@ -81,12 +80,12 @@ function ChatBox({ width, height }: { roomId: string, width: number, height: num
} }
}; };
const sendMessage = async (e: any) => { const sendMessage = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
if (!inputMessage.trim()) return; if (!inputMessage.trim()) return;
await sendMessageToChat(username, inputMessage); await sendMessageToChat(roomId, username, inputMessage);
setInputMessage(""); setInputMessage("");
fetchMessages(); fetchMessages(roomId);
}; };
return ( return (
@ -117,7 +116,7 @@ function ChatBox({ width, height }: { roomId: string, width: number, height: num
); );
} }
async function sendMessageToChat(username: string, content: string): Promise<void> { async function sendMessageToChat(roomId: string, username: string, content: string): Promise<void> {
const apiUrl = 'https://jeffemmett-realtimechatappwithpolling.web.val.run'; // Replace with your actual Val Town URL const apiUrl = 'https://jeffemmett-realtimechatappwithpolling.web.val.run'; // Replace with your actual Val Town URL
try { try {
@ -128,6 +127,7 @@ async function sendMessageToChat(username: string, content: string): Promise<voi
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
roomId,
username, username,
content, content,
}), }),

View File

@ -25,7 +25,7 @@
"noUnusedParameters": true, "noUnusedParameters": true,
"noFallthroughCasesInSwitch": true "noFallthroughCasesInSwitch": true
}, },
"include": ["src", "worker"], "include": ["src", "worker", "src/client"],
"references": [{ "path": "./tsconfig.node.json" }] "references": [{ "path": "./tsconfig.node.json" }]
} }

View File

@ -9,7 +9,7 @@ export { TldrawDurableObject } from './TldrawDurableObject'
// we use itty-router (https://itty.dev/) to handle routing. in this example we turn on CORS because // we use itty-router (https://itty.dev/) to handle routing. in this example we turn on CORS because
// we're hosting the worker separately to the client. you should restrict this to your own domain. // we're hosting the worker separately to the client. you should restrict this to your own domain.
const { preflight, corsify } = cors({ origin: '*' }) const { preflight, corsify } = cors({ origin: '*' })
const router = AutoRouter<IRequest, [env: Environment]>({ const router = AutoRouter<IRequest, [env: Environment, ctx: ExecutionContext]>({
before: [preflight], before: [preflight],
finally: [corsify], finally: [corsify],
catch: (e) => { catch: (e) => {