MCPcopy Create free account
hub / github.com/Noumena-Network/code / connect

Method connect

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

Callers 2

scheduleReconnectMethod · 0.95
reconnectMethod · 0.95

Calls 11

startPingIntervalMethod · 0.95
handleMessageMethod · 0.95
handleCloseMethod · 0.95
getWebSocketProxyUrlFunction · 0.85
getWebSocketTLSOptionsFunction · 0.85
getWebSocketProxyAgentFunction · 0.85
toStringMethod · 0.65
logForDebuggingFunction · 0.50
logErrorFunction · 0.50
onErrorMethod · 0.45

Tested by

no test coverage detected