|
| 1 | +import React, { useState, useEffect, useRef } from "react"; |
| 2 | +import { createClient, createRivetKit } from "@rivetkit/react"; |
| 3 | +import type { registry } from "../backend/registry"; |
| 4 | + |
| 5 | +const client = createClient<typeof registry>(); |
| 6 | +const { useActor } = createRivetKit(client); |
| 7 | + |
| 8 | +export default function App() { |
| 9 | + const [messages, setMessages] = useState<Array<{ id: string; text: string; timestamp: number }>>([]); |
| 10 | + const [inputText, setInputText] = useState(""); |
| 11 | + const [isConnected, setIsConnected] = useState(false); |
| 12 | + |
| 13 | + // Connect to the WebSocket actor |
| 14 | + const chatRoom = useActor({ |
| 15 | + name: "chatRoom", |
| 16 | + key: ["random"], |
| 17 | + }); |
| 18 | + |
| 19 | + // Raw WS we created to connect to the actors |
| 20 | + const wsRef = useRef<WebSocket | null>(null); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + (async () => { |
| 24 | + const ws = await chatRoom.handle?.websocket(); |
| 25 | + |
| 26 | + if (!ws) return; |
| 27 | + |
| 28 | + ws.onopen = () => { |
| 29 | + setIsConnected(true); |
| 30 | + console.log("Connected via direct access!"); |
| 31 | + }; |
| 32 | + |
| 33 | + ws.onmessage = (event) => { |
| 34 | + const data = JSON.parse(event.data); |
| 35 | + |
| 36 | + if (data.type === "init") { |
| 37 | + setMessages(data.messages); |
| 38 | + } else if (data.type === "message") { |
| 39 | + setMessages(prev => [...prev, { |
| 40 | + id: data.id, |
| 41 | + text: data.text, |
| 42 | + timestamp: data.timestamp |
| 43 | + }]); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + ws.onclose = (event) => { |
| 48 | + setIsConnected(false); |
| 49 | + console.log("WebSocket closed:", event.code, event.reason); |
| 50 | + }; |
| 51 | + |
| 52 | + ws.onerror = (event) => { |
| 53 | + console.error("WebSocket error:", event); |
| 54 | + }; |
| 55 | + |
| 56 | + wsRef.current = ws; |
| 57 | + })(); |
| 58 | + |
| 59 | + return () => { |
| 60 | + if (wsRef.current) { |
| 61 | + wsRef.current.close(); |
| 62 | + } |
| 63 | + }; |
| 64 | + }, [chatRoom.handle]); |
| 65 | + |
| 66 | + const sendMessage = (e: React.FormEvent) => { |
| 67 | + e.preventDefault(); |
| 68 | + if (!inputText.trim() || !wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) return; |
| 69 | + |
| 70 | + wsRef.current.send(JSON.stringify({ |
| 71 | + type: "message", |
| 72 | + text: inputText.trim() |
| 73 | + })); |
| 74 | + setInputText(""); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <div style={{ maxWidth: "800px", margin: "0 auto", padding: "20px" }}> |
| 79 | + <h1>Raw WebSocket Chat</h1> |
| 80 | + |
| 81 | + <div style={{ |
| 82 | + padding: "10px", |
| 83 | + background: isConnected ? "#4caf50" : "#f44336", |
| 84 | + color: "white", |
| 85 | + borderRadius: "4px", |
| 86 | + marginBottom: "20px" |
| 87 | + }}> |
| 88 | + {isConnected ? "Connected" : "Disconnected"} |
| 89 | + </div> |
| 90 | + |
| 91 | + <div style={{ |
| 92 | + background: "white", |
| 93 | + border: "1px solid #ddd", |
| 94 | + borderRadius: "8px", |
| 95 | + padding: "10px", |
| 96 | + height: "400px", |
| 97 | + overflowY: "auto", |
| 98 | + marginBottom: "10px" |
| 99 | + }}> |
| 100 | + {messages.map((msg) => ( |
| 101 | + <div key={msg.id} style={{ marginBottom: "10px" }}> |
| 102 | + <strong>{new Date(msg.timestamp).toLocaleTimeString()}:</strong> {msg.text} |
| 103 | + </div> |
| 104 | + ))} |
| 105 | + </div> |
| 106 | + |
| 107 | + <form onSubmit={sendMessage} style={{ display: "flex", gap: "10px" }}> |
| 108 | + <input |
| 109 | + type="text" |
| 110 | + value={inputText} |
| 111 | + onChange={(e) => setInputText(e.target.value)} |
| 112 | + placeholder="Type a message..." |
| 113 | + style={{ flex: 1, padding: "8px", borderRadius: "4px", border: "1px solid #ddd" }} |
| 114 | + /> |
| 115 | + <button type="submit">Send</button> |
| 116 | + </form> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
0 commit comments