2323 * SOFTWARE.
2424 */
2525
26- const express = require ( ' express' ) ;
27- const http = require ( ' http' ) ;
28- const { Server } = require ( ' socket.io' ) ;
26+ const express = require ( " express" ) ;
27+ const http = require ( " http" ) ;
28+ const { Server } = require ( " socket.io" ) ;
2929const app = express ( ) ;
3030const server = http . createServer ( app ) ;
31- const io = new Server ( server ,
32- {
33- transports : [ "websocket" ] ,
34- maxHttpBufferSize : 50 * 1024 * 1024
35- }
36- ) ;
37-
38- const PORT = 3001
31+ const io = new Server ( server , {
32+ transports : [ "websocket" , "polling" ] ,
33+ maxHttpBufferSize : 50 * 1024 * 1024 ,
34+ } ) ;
35+
36+ const PORT = 3001 ;
3937// Track clients by application
4038const applicationClients = { } ;
4139
42- io . on ( 'connection' , ( socket ) => {
43- console . log ( `User connected: ${ socket . id } ` ) ;
44-
45- socket . on ( 'register' , ( { application } ) => {
46- console . log ( `Client ${ socket . id } registered for application: ${ application } ` ) ;
47-
48- // Store the application preference with this socket
49- socket . data . application = application ;
50-
51- // Register this client for this application
52- if ( ! applicationClients [ application ] ) {
53- applicationClients [ application ] = new Set ( ) ;
54- }
55- applicationClients [ application ] . add ( socket . id ) ;
56-
57- // Optionally confirm registration
58- socket . emit ( 'registration_response' , {
59- type : 'registration' ,
60- status : 'success' ,
61- message : `Registered for ${ application } `
40+ io . on ( "connection" , ( socket ) => {
41+ console . log ( `User connected: ${ socket . id } ` ) ;
42+
43+ socket . on ( "register" , ( { application } ) => {
44+ console . log (
45+ `Client ${ socket . id } registered for application: ${ application } `
46+ ) ;
47+
48+ // Store the application preference with this socket
49+ socket . data . application = application ;
50+
51+ // Register this client for this application
52+ if ( ! applicationClients [ application ] ) {
53+ applicationClients [ application ] = new Set ( ) ;
54+ }
55+ applicationClients [ application ] . add ( socket . id ) ;
56+
57+ // Optionally confirm registration
58+ socket . emit ( "registration_response" , {
59+ type : "registration" ,
60+ status : "success" ,
61+ message : `Registered for ${ application } ` ,
62+ } ) ;
6263 } ) ;
63- } ) ;
64-
65- socket . on ( 'command_packet_response' , ( { packet } ) => {
66- const senderId = packet . senderId ;
67-
68- if ( senderId ) {
69-
70- io . to ( senderId ) . emit ( 'packet_response' , packet ) ;
71- console . log ( `Sent confirmation to client ${ senderId } ` ) ;
72- } else {
73- console . log ( `No sender ID provided in packet` ) ;
74- }
75- } ) ;
76-
77- socket . on ( 'command_packet' , ( { application, command } ) => {
78- console . log ( `Command from ${ socket . id } for application ${ application } :` , command ) ;
79-
80- // Register this client for this application if not already registered
81- //if (!applicationClients[application]) {
82- // applicationClients[application] = new Set();
83- //}
84- //applicationClients[application].add(socket.id);
85-
86- // Process the command
87-
88- let packet = {
89- senderId :socket . id ,
90- application :application ,
91- command :command
92- }
9364
94- sendToApplication ( packet )
95-
96- // Send response back to this client
97- //socket.emit('json_response', { from: 'server', command });
98- } ) ;
99-
100- socket . on ( 'disconnect' , ( ) => {
101- console . log ( `User disconnected: ${ socket . id } ` ) ;
102-
103- // Remove this client from all application registrations
104- for ( const app in applicationClients ) {
105- applicationClients [ app ] . delete ( socket . id ) ;
106- // Clean up empty sets
107- if ( applicationClients [ app ] . size === 0 ) {
108- delete applicationClients [ app ] ;
109- }
110- }
111- } ) ;
65+ socket . on ( "command_packet_response" , ( { packet } ) => {
66+ const senderId = packet . senderId ;
67+
68+ if ( senderId ) {
69+ io . to ( senderId ) . emit ( "packet_response" , packet ) ;
70+ console . log ( `Sent confirmation to client ${ senderId } ` ) ;
71+ } else {
72+ console . log ( `No sender ID provided in packet` ) ;
73+ }
74+ } ) ;
75+
76+ socket . on ( "command_packet" , ( { application, command } ) => {
77+ console . log (
78+ `Command from ${ socket . id } for application ${ application } :` ,
79+ command
80+ ) ;
81+
82+ // Register this client for this application if not already registered
83+ //if (!applicationClients[application]) {
84+ // applicationClients[application] = new Set();
85+ //}
86+ //applicationClients[application].add(socket.id);
87+
88+ // Process the command
89+
90+ let packet = {
91+ senderId : socket . id ,
92+ application : application ,
93+ command : command ,
94+ } ;
95+
96+ sendToApplication ( packet ) ;
97+
98+ // Send response back to this client
99+ //socket.emit('json_response', { from: 'server', command });
100+ } ) ;
101+
102+ socket . on ( "disconnect" , ( ) => {
103+ console . log ( `User disconnected: ${ socket . id } ` ) ;
104+
105+ // Remove this client from all application registrations
106+ for ( const app in applicationClients ) {
107+ applicationClients [ app ] . delete ( socket . id ) ;
108+ // Clean up empty sets
109+ if ( applicationClients [ app ] . size === 0 ) {
110+ delete applicationClients [ app ] ;
111+ }
112+ }
113+ } ) ;
112114} ) ;
113115
114116// Add a function to send messages to clients by application
115117function sendToApplication ( packet ) {
116-
117- let application = packet . application
118+ let application = packet . application ;
118119 if ( applicationClients [ application ] ) {
119- console . log ( `Sending to ${ applicationClients [ application ] . size } clients for ${ application } ` ) ;
120-
121- let senderId = packet . senderId
120+ console . log (
121+ `Sending to ${ applicationClients [ application ] . size } clients for ${ application } `
122+ ) ;
123+
124+ let senderId = packet . senderId ;
122125 // Loop through all client IDs for this application
123- applicationClients [ application ] . forEach ( clientId => {
124- io . to ( clientId ) . emit ( ' command_packet' , packet ) ;
125- } ) ;
126- return true ;
127- }
128- console . log ( `No clients registered for application: ${ application } ` ) ;
129- return false ;
126+ applicationClients [ application ] . forEach ( ( clientId ) => {
127+ io . to ( clientId ) . emit ( " command_packet" , packet ) ;
128+ } ) ;
129+ return true ;
130+ }
131+ console . log ( `No clients registered for application: ${ application } ` ) ;
132+ return false ;
130133}
131134
132135// Example: Use this function elsewhere in your code
133136// sendToApplication('photoshop', { message: 'Update available' });
134137
135138server . listen ( PORT , ( ) => {
136- console . log ( `adb-mcp Command proxy server running on ws://localhost:${ PORT } ` ) ;
137- } ) ;
139+ console . log (
140+ `adb-mcp Command proxy server running on ws://localhost:${ PORT } `
141+ ) ;
142+ } ) ;
0 commit comments