@@ -16,6 +16,7 @@ import * as path from "node:path";
1616import { Logger , LoggingDebugSession , TerminatedEvent , logger } from "@vscode/debugadapter" ;
1717import type { DebugProtocol } from "@vscode/debugprotocol" ;
1818import type { DebugSession } from "vscode" ;
19+ import { WebSocket } from "ws" ;
1920import {
2021 type DbgFile ,
2122 type DbgMap ,
@@ -77,7 +78,7 @@ export class Cc65DebugSession extends LoggingDebugSession {
7778
7879 private _program : ChildProcessWithoutNullStreams | undefined ;
7980
80- /// connected over TCP port?
81+ private _websocket : WebSocket | undefined ;
8182 private _connected = false ;
8283
8384 private _debugData : DbgMap | undefined ;
@@ -155,7 +156,8 @@ export class Cc65DebugSession extends LoggingDebugSession {
155156
156157 const header = `Content-Length: ${ contentLength } \r\n\r\n` ;
157158
158- this . _program ?. stdin . write ( header + json , "utf8" ) ;
159+ if ( this . _program ) this . _program ?. stdin . write ( header + json , "utf8" ) ;
160+ if ( this . _websocket ) this . _websocket ?. send ( header + json ) ;
159161 }
160162
161163 public sendResponse ( response : DebugProtocol . Response ) : void {
@@ -514,8 +516,52 @@ export class Cc65DebugSession extends LoggingDebugSession {
514516 /**
515517 * Attempts to connect to an existing debugger adapter.
516518 */
517- protected connectAdapter ( port : number , host : string ) : Promise < void > {
518- throw new Error ( "Connecting to existing debugger not supported yet." ) ;
519+ protected connectAdapter ( address : string | URL ) : Promise < void > {
520+ console . debug ( "connectAdapter" , address ) ;
521+
522+ const addr = typeof address === "string" ? new URL ( address ) : address ;
523+
524+ if ( [ "ws:" , "wss:" ] . includes ( addr . protocol ) ) {
525+ return new Promise ( ( resolve , reject ) => {
526+ const ws = new WebSocket ( addr ) ;
527+
528+ ws . addEventListener ( "open" , ( event ) => {
529+ console . log ( `WebSocket connection established to ${ addr } ` ) ;
530+ logger . log ( `WebSocket connection established to ${ addr } ` ) ;
531+ this . _websocket = ws ;
532+ this . _connected = true ;
533+ resolve ( ) ;
534+ } ) ;
535+
536+ ws . addEventListener ( "message" , ( event ) => {
537+ if ( typeof event . data === "string" || event . data instanceof String ) {
538+ this . _processData ( Buffer . from ( String ( event . data ) , "utf-8" ) ) ;
539+ } else {
540+ console . warn ( "Received non-text websocket data, ignoring." ) ;
541+ logger . warn ( "Received non-text websocket data, ignoring." ) ;
542+ }
543+ } ) ;
544+
545+ ws . addEventListener ( "error" , ( error ) => {
546+ console . error ( `WebSocket error: ${ error } ` ) ;
547+ logger . error ( `WebSocket error: ${ error } ` ) ;
548+ reject ( error . message ) ;
549+ } ) ;
550+
551+ ws . addEventListener ( "close" , ( event ) => {
552+ console . log (
553+ `WebSocket connection closed. Code: ${ event . code } , Reason: ${ event . reason } ` ,
554+ ) ;
555+ logger . log (
556+ `WebSocket connection closed. Code: ${ event . code } , Reason: ${ event . reason } ` ,
557+ ) ;
558+ } ) ;
559+ } ) ;
560+ }
561+
562+ throw new Error (
563+ `Unsupported connection protocol: ${ addr . protocol } . Only 'ws:' and 'wss:' are supported` ,
564+ ) ;
519565 }
520566
521567 /**
@@ -553,9 +599,9 @@ export class Cc65DebugSession extends LoggingDebugSession {
553599
554600 case "attach" :
555601 {
556- const { port , host = "localhost" } = this . _session . configuration ;
602+ const { address } = this . _session . configuration ;
557603 try {
558- await this . connectAdapter ( port , host ) ;
604+ await this . connectAdapter ( address ) ;
559605 } catch ( error ) {
560606 return this . sendErrorResponse ( response , {
561607 id : ErrorCodes . DAP_CONNECT_ERROR ,
@@ -591,11 +637,13 @@ export class Cc65DebugSession extends LoggingDebugSession {
591637 request ?: DebugProtocol . Request ,
592638 ) {
593639 console . log ( "attachRequest" , args ) ;
594- return this . sendErrorResponse ( response , {
595- id : ErrorCodes . DAP_NOT_SUPPORTED ,
596- format : "Attaching to TCP port is not yet supported." ,
597- showUser : true ,
598- } ) ;
640+
641+ if ( ! this . _connected )
642+ return this . sendErrorResponse ( response , {
643+ id : ErrorCodes . DAP_CONNECT_ERROR ,
644+ format : "Connection to the debug server was not established" ,
645+ showUser : true ,
646+ } ) ;
599647 }
600648
601649 protected disconnectRequest (
0 commit comments