implemented full binary progress system using websockets

This commit is contained in:
donny committed 2026-01-03 17:18:38 -07:00
1 parent 43ebd851ad
commit 6711e006c1
3 files changed
+34 -26

No files matched your search

+27 -22
View File
@@ -2,38 +2,43 @@ from server import PromptServer
from .progress import add_progress_hook
from .routes import SetupRoutes
from .workflows import *
import base64
from io import BytesIO
import json
# Custom binary event types (use high numbers to avoid conflicts)
IMAGELAB_PROGRESS_EVENT = 100
IMAGELAB_PREVIEW_EVENT = 101
# Your custom progress callback
def my_progress_callback(value, total, preview_image, prompt_id, node_id):
# print(f"Progress: {value}/{total}, Prompt: {prompt_id}, Node: {node_id}, Preview: {preview_image}")
# Send progress data as binary JSON
progress_data = {
'value': value,
'total': total,
'prompt_id': prompt_id,
'node_id': node_id,
'progress_percent': round((value / total * 100), 2) if total > 0 else 0
}
progress_bytes = json.dumps(progress_data).encode('utf-8')
PromptServer.instance.send_sync(IMAGELAB_PROGRESS_EVENT, progress_bytes, sid=None)
# Convert preview image to base64 if available
preview_base64 = None
# Send preview image separately if available
if preview_image is not None:
try:
# preview_image is a tuple: (format, PIL.Image, max_size)
if isinstance(preview_image, tuple) and len(preview_image) >= 2:
pil_image = preview_image[1] # Extract the PIL Image
else:
pil_image = preview_image
buffer = BytesIO()
pil_image.save(buffer, format='JPEG', quality=80, optimize=True)
preview_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
image_format = preview_image[0] # 'JPEG' or 'PNG'
pil_image = preview_image[1]
# Convert PIL image to bytes
buffer = BytesIO()
pil_image.save(buffer, format=image_format, quality=95 if image_format == 'JPEG' else None)
image_bytes = buffer.getvalue()
# Send as binary message with integer event type
PromptServer.instance.send_sync(IMAGELAB_PREVIEW_EVENT, image_bytes, sid=None)
except Exception as e:
print(f"[ImageLab] Error encoding preview image: {e}")
# Broadcast to ALL WebSocket clients using ComfyUI's system (sid=None means broadcast to all)
PromptServer.instance.send_sync("imagelab_progress", {
'value': value,
'total': total,
'preview_image': preview_base64, # base64 encoded JPEG or None
'prompt_id': prompt_id,
'node_id': node_id,
'progress_percent': round((value / total * 100), 2) if total > 0 else 0
}, sid=None)
print(f"[ImageLab] Error sending preview image: {e}")
add_progress_hook(my_progress_callback)