import WebSocket from "ws"; const ws = new WebSocket("ws://localhost:8080"); ws.on("open", () => { console.log("✅ 已连接 WebSocket"); ws.send(JSON.stringify({ type: "auth", app: "demo" })); ws.send(JSON.stringify({ type: "subscribe", channel: "chat" })); setTimeout(() => { ws.send( JSON.stringify({ type: "publish", app: "demo", channel: "chat", event: "message", data: { text: "Hello Redis WS 🚀" }, }) ); }, 2000); }); ws.on("message", (msg) => { console.log("📩 收到消息:", msg.toString()); });