MCPcopy Index your code
hub / github.com/agent0ai/agent-zero / initializeSocket

Method initializeSocket

webui/js/websocket.js:618–692  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

616 }
617
618 initializeSocket() {
619 this.socket = io(this.namespace, {
620 autoConnect: false,
621 reconnection: true,
622 transports: ["websocket", "polling"],
623 withCredentials: true,
624 auth: (cb) => {
625 const handlers = [...this._handlers];
626 getCsrfToken()
627 .then((token) => cb({ csrf_token: token, handlers }))
628 .catch((error) => {
629 console.error("[websocket] failed to fetch CSRF token for connect", error);
630 cb({ handlers });
631 });
632 },
633 });
634
635 this.socket.on("connect", () => {
636 this.connected = true;
637 this._csrfInvalidatedForConnectError = false;
638 this._connectErrorRetryAttempt = 0;
639 this._clearConnectErrorRetryTimer();
640
641 const runtimeId = getRuntimeId();
642 const runtimeChanged = Boolean(
643 this._lastRuntimeId &&
644 runtimeId &&
645 this._lastRuntimeId !== runtimeId
646 );
647 const firstConnect = !this._hasConnectedOnce;
648 this._hasConnectedOnce = true;
649 this._lastRuntimeId = runtimeId;
650
651 this.debugLog("socket connected", {
652 sid: this.socket.id,
653 runtimeId,
654 runtimeChanged,
655 firstConnect,
656 });
657 this.connectCallbacks.forEach((cb) => {
658 try {
659 cb({ runtimeId, runtimeChanged, firstConnect });
660 } catch (error) {
661 console.error("WebSocket onConnect callback error:", error);
662 }
663 });
664 });
665
666 this.socket.on("disconnect", (reason) => {
667 this.connected = false;
668 this.debugLog("socket disconnected", { reason });
669 this.disconnectCallbacks.forEach((cb) => {
670 try {
671 cb(reason);
672 } catch (error) {
673 console.error("WebSocket onDisconnect callback error:", error);
674 }
675 });

Callers 1

connectMethod · 0.95

Calls 10

debugLogMethod · 0.95
invokeErrorCallbacksMethod · 0.95
ioClass · 0.90
getCsrfTokenFunction · 0.90
getRuntimeIdFunction · 0.90
invalidateCsrfTokenFunction · 0.90
errorMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected