MCPcopy
hub / github.com/Sathvik-Rao/ClipCascade / connect

Method connect

ClipCascade_Desktop/src/p2p/p2p_manager.py:145–207  ·  view source on GitHub ↗

Connects to the P2P websocket signaling server.

(self)

Source from the content-addressed store, hash-verified

143 return (RECONNECT_WS_TIMER * 1000) + WEBSOCKET_TIMEOUT
144
145 def connect(self) -> tuple[bool, str]:
146 """
147 Connects to the P2P websocket signaling server.
148 """
149 try:
150 if self.ws_client is not None:
151 if self.is_connected:
152 return True, ""
153 try:
154 self.ws_client.close()
155 except Exception:
156 pass
157 self.ws_client = None
158
159 self.ws_client = websocket.WebSocketApp(
160 url=self.config.data["websocket_url"],
161 header={"Cookie": RequestManager.format_cookie(self.config.data["cookie"])},
162 on_open=self._on_ws_open,
163 on_error=self._on_ws_error,
164 on_message=self._on_ws_message,
165 on_close=self._on_ws_close,
166 )
167
168 ws_run_kw = {
169 "ping_interval": P2P_WS_PING_INTERVAL_SEC,
170 "ping_timeout": P2P_WS_PING_TIMEOUT_SEC,
171 }
172 ws_sslopt = websocket_sslopt_for_config(self.config)
173 if ws_sslopt:
174 ws_run_kw["sslopt"] = ws_sslopt
175 Thread(
176 target=self.ws_client.run_forever,
177 kwargs=ws_run_kw,
178 daemon=True,
179 ).start()
180
181 if not self.is_clipboard_monitoring_on:
182 # Start clipboard monitoring
183 self.is_clipboard_monitoring_on = True
184 self.clipboard_manager.on_copy(self.send)
185
186 if not self.is_login_phase:
187 return True, ""
188
189 total_ms = 0
190 while self.is_connected is False:
191 time.sleep(0.25)
192 total_ms += 250
193 if 0 < WEBSOCKET_TIMEOUT < total_ms:
194 self.ws_client.close()
195 raise TimeoutError(
196 f"Connection to {self.config.data['websocket_url']} timed out"
197 )
198
199 return True, "Websocket connected"
200 except Exception as e:
201 msg = f"Failed to connect websocket: {e}"
202 logging.error(msg)

Callers 2

_on_ws_closeMethod · 0.95
manual_reconnectMethod · 0.95

Calls 6

ws_closeMethod · 0.95
format_cookieMethod · 0.80
startMethod · 0.80
on_copyMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected