Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
ComfyUI
Commits
2eaa6640
Unverified
Commit
2eaa6640
authored
Mar 02, 2023
by
pythongosssss
Committed by
GitHub
Mar 02, 2023
Browse files
Refactored sockets
parent
5e25c770
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
1 deletion
+80
-1
web/scripts/api.js
web/scripts/api.js
+80
-1
No files found.
web/scripts/api.js
View file @
2eaa6640
class
ComfyApi
{
class
ComfyApi
extends
EventTarget
{
constructor
()
{
super
();
}
#
pollQueue
()
{
setInterval
(
async
()
=>
{
try
{
const
resp
=
await
fetch
(
"
/prompt
"
);
const
status
=
await
resp
.
json
();
this
.
dispatchEvent
(
new
CustomEvent
(
"
status
"
,
{
detail
:
status
}));
}
catch
(
error
)
{
this
.
dispatchEvent
(
new
CustomEvent
(
"
status
"
,
{
detail
:
null
}));
}
},
1000
);
}
#
createSocket
(
isReconnect
)
{
if
(
this
.
socket
)
{
return
;
}
let
opened
=
false
;
this
.
socket
=
new
WebSocket
(
`ws
${
window
.
location
.
protocol
===
"
https:
"
?
"
s
"
:
""
}
://
${
location
.
host
}
/ws`
);
this
.
socket
.
addEventListener
(
"
open
"
,
()
=>
{
opened
=
true
;
if
(
isReconnect
)
{
this
.
dispatchEvent
(
new
CustomEvent
(
"
reconnected
"
));
}
});
this
.
socket
.
addEventListener
(
"
error
"
,
()
=>
{
if
(
this
.
socket
)
this
.
socket
.
close
();
this
.
#
pollQueue
();
});
this
.
socket
.
addEventListener
(
"
close
"
,
()
=>
{
setTimeout
(()
=>
{
this
.
socket
=
null
;
this
.
#
createSocket
(
true
);
},
300
);
if
(
opened
)
{
this
.
dispatchEvent
(
new
CustomEvent
(
"
status
"
,
{
detail
:
null
}));
this
.
dispatchEvent
(
new
CustomEvent
(
"
reconnecting
"
));
}
});
this
.
socket
.
addEventListener
(
"
message
"
,
(
event
)
=>
{
try
{
const
msg
=
JSON
.
parse
(
event
.
data
);
switch
(
msg
.
type
)
{
case
"
status
"
:
if
(
msg
.
data
.
sid
)
{
this
.
clientId
=
msg
.
data
.
sid
;
}
this
.
dispatchEvent
(
new
CustomEvent
(
"
status
"
,
{
detail
:
msg
.
data
.
status
}));
break
;
case
"
progress
"
:
this
.
dispatchEvent
(
new
CustomEvent
(
"
progress
"
,
{
detail
:
msg
.
data
}));
break
;
case
"
executing
"
:
this
.
dispatchEvent
(
new
CustomEvent
(
"
executing
"
,
{
detail
:
msg
.
data
.
node
}));
break
;
case
"
executed
"
:
this
.
dispatchEvent
(
new
CustomEvent
(
"
executed
"
,
{
detail
:
msg
.
data
}));
break
;
default
:
throw
new
Error
(
"
Unknown message type
"
);
}
}
catch
(
error
)
{
console
.
warn
(
"
Unhandled message:
"
,
event
.
data
);
}
});
}
init
()
{
this
.
#
createSocket
();
}
async
getNodeDefs
()
{
async
getNodeDefs
()
{
const
resp
=
await
fetch
(
"
object_info
"
,
{
cache
:
"
no-store
"
});
const
resp
=
await
fetch
(
"
object_info
"
,
{
cache
:
"
no-store
"
});
return
await
resp
.
json
();
return
await
resp
.
json
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment