@@ -116,7 +116,14 @@ const start = async (): Promise<number> => {
116116 Log . info ( `${ clientType } .msg.${ deviceId } ` , data )
117117 }
118118 } else if ( clientType === 'DeviceMirror' ) {
119- if ( data . type === 'ready' ) {
119+ if ( data . type === 'preview' ) {
120+ // 投屏进程也负责发送设备卡片预览图(投屏期间不再单独启动预览进程)
121+ broadcast ( 'Render' , {
122+ type : 'DevicePreview' ,
123+ deviceId,
124+ data : data . data ,
125+ } )
126+ } else if ( data . type === 'ready' ) {
120127 ws . send ( JSON . stringify ( getPanelConfig ( deviceId ) ) )
121128 Log . info ( `Sent panel config to DeviceMirror: deviceId=${ deviceId } ` )
122129 // 查询设备实际屏幕状态,确保图标与真实状态一致
@@ -312,6 +319,30 @@ const panelButtonToKeyCode = (buttonId: string): number | null => {
312319 return keyCodeMap [ buttonId ] || null
313320}
314321
322+ // 随动目标:其它设备的预览进程或投屏进程(投屏期间预览进程已停止);
323+ // 同一设备可能同时有主投屏与应用投屏多个连接,每台设备只下发一次,避免手机上执行两次
324+ const followTargets = ( sourceDeviceId : string ) : Array < { ws : WebSocket ; deviceId : string ; type : string } > => {
325+ const targets : Array < { ws : WebSocket ; deviceId : string ; type : string } > = [ ]
326+ const seen = new Set < string > ( )
327+ wss ?. clients . forEach ( ( client ) => {
328+ const ws = client as any
329+ const clientDeviceId = ws . _deviceId
330+ const clientType = ws . _clientType
331+ if ( clientType !== 'DeviceManage' && clientType !== 'DeviceMirror' ) {
332+ return
333+ }
334+ if ( ! clientDeviceId || clientDeviceId === sourceDeviceId || seen . has ( clientDeviceId ) ) {
335+ return
336+ }
337+ if ( client . readyState !== WebSocket . OPEN ) {
338+ return
339+ }
340+ seen . add ( clientDeviceId )
341+ targets . push ( { ws : client , deviceId : clientDeviceId , type : clientType } )
342+ } )
343+ return targets
344+ }
345+
315346// 处理面板按钮的随动模式 - 将按钮操作转换为key事件并广播
316347const handlePanelButtonFollowMode = ( sourceDeviceId : string , buttonId : string ) => {
317348 const keyCode = panelButtonToKeyCode ( buttonId )
@@ -343,25 +374,18 @@ const handlePanelButtonFollowMode = (sourceDeviceId: string, buttonId: string) =
343374 }
344375
345376 Log . info ( `FollowMode: Broadcasting panel button from ${ sourceDeviceId } ` , buttonId )
346- let targetCount = 0
347- wss ?. clients . forEach ( ( client ) => {
348- if ( client . readyState === WebSocket . OPEN ) {
349- const ws = client as any
350- const clientDeviceId = ws . _deviceId
351- const clientType = ws . _clientType
352- if ( clientType === 'DeviceManage' && clientDeviceId && clientDeviceId !== sourceDeviceId ) {
353- try {
354- // 发送按下和抬起两个事件
355- client . send ( JSON . stringify ( keyDownEvent ) )
356- client . send ( JSON . stringify ( keyUpEvent ) )
357- targetCount ++
358- Log . info ( `FollowMode: Panel button sent to ${ clientType } :${ clientDeviceId } ${ buttonId } ->${ keyCode } ` )
359- } catch ( error ) {
360- Log . error ( `FollowMode: Failed to send panel button to ${ clientDeviceId } : ${ error . message } ` )
361- }
362- }
377+ const targets = followTargets ( sourceDeviceId )
378+ for ( const target of targets ) {
379+ try {
380+ // 发送按下和抬起两个事件
381+ target . ws . send ( JSON . stringify ( keyDownEvent ) )
382+ target . ws . send ( JSON . stringify ( keyUpEvent ) )
383+ Log . info ( `FollowMode: Panel button sent to ${ target . type } :${ target . deviceId } ${ buttonId } ->${ keyCode } ` )
384+ } catch ( error ) {
385+ Log . error ( `FollowMode: Failed to send panel button to ${ target . deviceId } : ${ error . message } ` )
363386 }
364- } )
387+ }
388+ const targetCount = targets . length
365389
366390 if ( targetCount === 0 ) {
367391 Log . info ( `FollowMode: No target devices to forward panel button (source: ${ sourceDeviceId } )` )
@@ -376,23 +400,16 @@ const broadcastRotateToOtherDevices = (sourceDeviceId: string) => {
376400 }
377401
378402 Log . info ( `FollowMode: Broadcasting rotate from ${ sourceDeviceId } ` )
379- let targetCount = 0
380- wss ?. clients . forEach ( ( client ) => {
381- if ( client . readyState === WebSocket . OPEN ) {
382- const ws = client as any
383- const clientDeviceId = ws . _deviceId
384- const clientType = ws . _clientType
385- if ( clientType === 'DeviceManage' && clientDeviceId && clientDeviceId !== sourceDeviceId ) {
386- try {
387- client . send ( JSON . stringify ( rotateEvent ) )
388- targetCount ++
389- Log . info ( `FollowMode: Rotate sent to ${ clientType } :${ clientDeviceId } ` )
390- } catch ( error ) {
391- Log . error ( `FollowMode: Failed to send rotate to ${ clientDeviceId } : ${ error . message } ` )
392- }
393- }
403+ const targets = followTargets ( sourceDeviceId )
404+ for ( const target of targets ) {
405+ try {
406+ target . ws . send ( JSON . stringify ( rotateEvent ) )
407+ Log . info ( `FollowMode: Rotate sent to ${ target . type } :${ target . deviceId } ` )
408+ } catch ( error ) {
409+ Log . error ( `FollowMode: Failed to send rotate to ${ target . deviceId } : ${ error . message } ` )
394410 }
395- } )
411+ }
412+ const targetCount = targets . length
396413
397414 if ( targetCount === 0 ) {
398415 Log . info ( `FollowMode: No target devices to forward rotate (source: ${ sourceDeviceId } )` )
@@ -402,23 +419,16 @@ const broadcastRotateToOtherDevices = (sourceDeviceId: string) => {
402419// 处理随动模式 - 在main进程中直接转发事件给其他设备,不经过Render进程
403420const handleFollowMode = ( sourceDeviceId : string , eventData : any ) => {
404421 Log . info ( `FollowMode: Broadcasting event from ${ sourceDeviceId } ` , eventData . type )
405- let targetCount = 0
406- wss ?. clients . forEach ( ( client ) => {
407- if ( client . readyState === WebSocket . OPEN ) {
408- const ws = client as any
409- const clientDeviceId = ws . _deviceId
410- const clientType = ws . _clientType
411- if ( clientType === 'DeviceManage' && clientDeviceId && clientDeviceId !== sourceDeviceId ) {
412- try {
413- client . send ( JSON . stringify ( eventData ) )
414- targetCount ++
415- Log . info ( `FollowMode: Event sent to ${ clientType } :${ clientDeviceId } ${ JSON . stringify ( eventData ) } ` )
416- } catch ( error ) {
417- Log . error ( `FollowMode: Failed to send event to ${ clientDeviceId } : ${ error . message } ` )
418- }
419- }
422+ const targets = followTargets ( sourceDeviceId )
423+ for ( const target of targets ) {
424+ try {
425+ target . ws . send ( JSON . stringify ( eventData ) )
426+ Log . info ( `FollowMode: Event sent to ${ target . type } :${ target . deviceId } ${ JSON . stringify ( eventData ) } ` )
427+ } catch ( error ) {
428+ Log . error ( `FollowMode: Failed to send event to ${ target . deviceId } : ${ error . message } ` )
420429 }
421- } )
430+ }
431+ const targetCount = targets . length
422432
423433 if ( targetCount === 0 ) {
424434 Log . info ( `FollowMode: No target devices to forward (source: ${ sourceDeviceId } )` )
0 commit comments