refactor: Unify sync server and combine torch with connection UI
- Consolidate git and torch WebSocket servers on port 4105 - Create separate handlers for git and torch in handlers/ directory - Combine TorchButton with connection status into single pill button - Remove StatusBar (now redundant with TorchButton) - Remove auto-assign torch on register/disconnect - Remove auto-connect to MCP on page load - Connection only happens when user explicitly requests torch
This commit is contained in:
143
server/services/handlers/torch-handler.ts
Normal file
143
server/services/handlers/torch-handler.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* Torch Handler
|
||||
* Handles multi-browser MCP control synchronization for the sync server.
|
||||
* Only one browser can have the "torch" and connect to MCP at a time.
|
||||
*/
|
||||
|
||||
// Client metadata
|
||||
interface TorchClient {
|
||||
ws: any
|
||||
id: string
|
||||
userAgent: string
|
||||
hostname: string
|
||||
connectedAt: Date
|
||||
}
|
||||
|
||||
// Connected torch clients (separate tracking from main clients set)
|
||||
const torchClients = new Map<any, TorchClient>()
|
||||
let clientIdCounter = 1
|
||||
|
||||
// Torch state - who has control
|
||||
let torchHolderId: string | null = null
|
||||
|
||||
function generateClientId(): string {
|
||||
return `client_${clientIdCounter++}_${Date.now().toString(36)}`
|
||||
}
|
||||
|
||||
function broadcastTorchState(broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const clientList = Array.from(torchClients.values()).map(c => ({
|
||||
id: c.id,
|
||||
userAgent: c.userAgent,
|
||||
hostname: c.hostname,
|
||||
connectedAt: c.connectedAt.toISOString(),
|
||||
hasTorch: c.id === torchHolderId
|
||||
}))
|
||||
|
||||
const message = JSON.stringify({
|
||||
type: 'torch-update',
|
||||
holderId: torchHolderId,
|
||||
clients: clientList
|
||||
})
|
||||
|
||||
broadcast(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle torch client connection
|
||||
*/
|
||||
export function handleTorchConnect(ws: any, broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const id = generateClientId()
|
||||
torchClients.set(ws, {
|
||||
ws,
|
||||
id,
|
||||
userAgent: 'Unknown',
|
||||
hostname: 'Unknown',
|
||||
connectedAt: new Date()
|
||||
})
|
||||
console.log(`[Torch] Client connected: ${id} (${torchClients.size} total)`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle torch messages
|
||||
*/
|
||||
export function handleTorchMessage(ws: any, data: any, broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const client = torchClients.get(ws)
|
||||
if (!client) return
|
||||
|
||||
switch (data.type) {
|
||||
case 'register': {
|
||||
client.userAgent = data.userAgent || 'Unknown'
|
||||
client.hostname = data.hostname || 'Unknown'
|
||||
|
||||
// No auto-assign - torch must be explicitly requested
|
||||
const hasTorch = torchHolderId === client.id
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: 'registered',
|
||||
id: client.id,
|
||||
hasTorch
|
||||
}))
|
||||
|
||||
console.log(`[Torch] Registered: ${client.id} (torch: ${hasTorch})`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
|
||||
case 'request': {
|
||||
const previousHolder = torchHolderId
|
||||
torchHolderId = client.id
|
||||
|
||||
ws.send(JSON.stringify({ type: 'granted' }))
|
||||
console.log(`[Torch] Transferred: ${previousHolder} → ${client.id}`)
|
||||
broadcastTorchState(broadcast)
|
||||
break
|
||||
}
|
||||
|
||||
case 'release': {
|
||||
if (torchHolderId === client.id) {
|
||||
torchHolderId = null
|
||||
ws.send(JSON.stringify({ type: 'released' }))
|
||||
console.log(`[Torch] Released by: ${client.id}`)
|
||||
broadcastTorchState(broadcast)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle torch client disconnect
|
||||
*/
|
||||
export function handleTorchDisconnect(ws: any, broadcast: (message: string, filter?: (ws: any) => boolean) => void) {
|
||||
const client = torchClients.get(ws)
|
||||
if (client) {
|
||||
console.log(`[Torch] Client disconnected: ${client.id}`)
|
||||
|
||||
// If this client had the torch, release it (no auto-assign)
|
||||
if (torchHolderId === client.id) {
|
||||
torchHolderId = null
|
||||
console.log(`[Torch] Torch released (holder disconnected)`)
|
||||
}
|
||||
|
||||
torchClients.delete(ws)
|
||||
broadcastTorchState(broadcast)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get torch status for health check
|
||||
*/
|
||||
export function getTorchStatus() {
|
||||
return {
|
||||
clients: torchClients.size,
|
||||
torchHolder: torchHolderId
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup torch handler
|
||||
*/
|
||||
export function cleanupTorchHandler() {
|
||||
torchClients.clear()
|
||||
torchHolderId = null
|
||||
}
|
||||
Reference in New Issue
Block a user