Spaces:
Sleeping
Sleeping
Download server/static/js/modules/api.js from ArshPathan/material_forge_env: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/ArshPathan/material_forge_env/resolve/main/server/static/js/modules/api.js
- Command line
-
hf download hf://spaces/ArshPathan/material_forge_env/server/static/js/modules/api.js
-
curl -L -o api.js https://huggingface.co/spaces/ArshPathan/material_forge_env/resolve/main/server/static/js/modules/api.js
1.06 kB
| /** | |
| * MaterialForge API & Socket Module | |
| * logic for backend communication. | |
| */ | |
| export class LabAPI { | |
| constructor(baseUrl) { | |
| this.baseUrl = baseUrl; | |
| this.socket = null; | |
| this.onMessageHandlers = []; | |
| } | |
| connect(path) { | |
| const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; | |
| const wsUrl = `${protocol}//${window.location.host}${path}`; | |
| return new Promise((resolve, reject) => { | |
| this.socket = new WebSocket(wsUrl); | |
| this.socket.onopen = () => { | |
| console.log('[API] Connected to lab engine'); | |
| resolve(); | |
| }; | |
| this.socket.onerror = (err) => reject(err); | |
| this.socket.onmessage = (msg) => { | |
| const data = JSON.parse(msg.data); | |
| this.onMessageHandlers.forEach(h => h(data)); | |
| }; | |
| }); | |
| } | |
| send(type, data) { | |
| if (this.socket && this.socket.readyState === WebSocket.OPEN) { | |
| this.socket.send(JSON.stringify({ type, data })); | |
| } | |
| } | |
| onMessage(handler) { | |
| this.onMessageHandlers.push(handler); | |
| } | |
| } | |