Download custom_nodes/ComfyUI-Serving-Toolkit/examples/websocket.js from StupidGame/MyComfySetUp: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/StupidGame/MyComfySetUp/resolve/main/custom_nodes/ComfyUI-Serving-Toolkit/examples/websocket.js
- Command line
-
hf download hf://StupidGame/MyComfySetUp/custom_nodes/ComfyUI-Serving-Toolkit/examples/websocket.js
-
curl -L -o websocket.js https://huggingface.co/StupidGame/MyComfySetUp/resolve/main/custom_nodes/ComfyUI-Serving-Toolkit/examples/websocket.js
1.42 kB
| import WebSocket, { WebSocketServer } from 'ws'; | |
| import fs from 'fs'; | |
| const wss = new WebSocketServer({ | |
| port: 8080, | |
| }); | |
| wss.on("connection", (ws) => { | |
| console.log("WebSocket client connected"); | |
| // Event listener for receiving messages from the client | |
| ws.on("message", (message) => { | |
| try { | |
| const parsed = JSON.parse(message) | |
| saveBase64Image(parsed.base64_img, parsed._requestId + '_image.webp'); | |
| } catch (e) { | |
| console.log("Error occured when getting a message", e) | |
| } | |
| }); | |
| // Event listener for the WebSocket connection closing | |
| ws.on("close", () => { | |
| console.log("WebSocket client disconnected"); | |
| }); | |
| }); | |
| function saveBase64Image(base64String, filePath) { | |
| const binaryData = Buffer.from(base64String, 'base64'); | |
| fs.writeFile(filePath, binaryData, 'binary', (err) => { | |
| if (err) { | |
| console.error('Error saving the image:', err); | |
| } else { | |
| console.log('Image saved successfully:', filePath); | |
| } | |
| }); | |
| } | |
| console.log("Listening on 8080") | |
| function sendMessage(message) { | |
| wss.clients.forEach((client) => { | |
| console.log("Messaging Everyone a hi") | |
| if (client.readyState === WebSocket.OPEN) { | |
| client.send(message); | |
| } | |
| }); | |
| } | |
| let i = 0 | |
| setInterval(() => sendMessage(JSON.stringify({ | |
| _requestId: ++i, | |
| prompt: "Cow" | |
| })), 5000) |