MCPcopy
hub / github.com/codeaashu/claude-code / connect

Method connect

src/remote/SessionsWebSocket.ts:100–205  ·  view source on GitHub ↗

* Connect to the sessions WebSocket endpoint

()

Source from the content-addressed store, hash-verified

98 * Connect to the sessions WebSocket endpoint
99 */
100 async connect(): Promise<void> {
101 if (this.state === 'connecting') {
102 logForDebugging('[SessionsWebSocket] Already connecting')
103 return
104 }
105
106 this.state = 'connecting'
107
108 const baseUrl = getOauthConfig().BASE_API_URL.replace('https://', 'wss://')
109 const url = `${baseUrl}/v1/sessions/ws/${this.sessionId}/subscribe?organization_uuid=${this.orgUuid}`
110
111 logForDebugging(`[SessionsWebSocket] Connecting to ${url}`)
112
113 // Get fresh token for each connection attempt
114 const accessToken = this.getAccessToken()
115 const headers = {
116 Authorization: `Bearer ${accessToken}`,
117 'anthropic-version': '2023-06-01',
118 }
119
120 if (typeof Bun !== 'undefined') {
121 // Bun's WebSocket supports headers/proxy options but the DOM typings don't
122 // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins
123 const ws = new globalThis.WebSocket(url, {
124 headers,
125 proxy: getWebSocketProxyUrl(url),
126 tls: getWebSocketTLSOptions() || undefined,
127 } as unknown as string[])
128 this.ws = ws
129
130 ws.addEventListener('open', () => {
131 logForDebugging(
132 '[SessionsWebSocket] Connection opened, authenticated via headers',
133 )
134 this.state = 'connected'
135 this.reconnectAttempts = 0
136 this.sessionNotFoundRetries = 0
137 this.startPingInterval()
138 this.callbacks.onConnected?.()
139 })
140
141 ws.addEventListener('message', (event: MessageEvent) => {
142 const data =
143 typeof event.data === 'string' ? event.data : String(event.data)
144 this.handleMessage(data)
145 })
146
147 ws.addEventListener('error', () => {
148 const err = new Error('[SessionsWebSocket] WebSocket error')
149 logError(err)
150 this.callbacks.onError?.(err)
151 })
152
153 // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins
154 ws.addEventListener('close', (event: CloseEvent) => {
155 logForDebugging(
156 `[SessionsWebSocket] Closed: code=${event.code} reason=${event.reason}`,
157 )

Callers 2

scheduleReconnectMethod · 0.95
reconnectMethod · 0.95

Calls 12

startPingIntervalMethod · 0.95
handleMessageMethod · 0.95
handleCloseMethod · 0.95
logForDebuggingFunction · 0.85
getOauthConfigFunction · 0.85
getWebSocketProxyUrlFunction · 0.85
getWebSocketTLSOptionsFunction · 0.85
getWebSocketProxyAgentFunction · 0.85
onErrorMethod · 0.80
onMethod · 0.80
toStringMethod · 0.65
logErrorFunction · 0.50

Tested by

no test coverage detected