Skip to content
This repository was archived by the owner on Apr 24, 2026. It is now read-only.

Commit e85337b

Browse files
committed
Enhance WebSocket connection handling with detailed logging for connections, messages, and errors
1 parent bccfad1 commit e85337b

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

agent-manager/src/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,29 @@ function sendError(ws, code, message) {
943943
// ---------------------------------------------------------------------------
944944
// WebSocket connection handler
945945
// ---------------------------------------------------------------------------
946-
wss.on('connection', (ws) => {
946+
wss.on('connection', (ws, req) => {
947+
console.log('New WebSocket connection from:', req.socket.remoteAddress)
947948
let agentId = null
948949

949950
ws.on('message', async (raw, isBinary) => {
951+
console.log('Received message, isBinary:', isBinary, 'size:', raw.length)
952+
// Track activity for inactivity timeout
953+
if (agentId) {
954+
const session = agentSessions.get(agentId)
955+
if (session) session.lastActivity = Date.now()
956+
}
957+
958+
// Log message type for debugging
959+
if (!isBinary) {
960+
try {
961+
const msg = JSON.parse(raw)
962+
console.log('WS message type:', msg.type)
963+
} catch (e) {
964+
console.log('WS text message (not JSON):', raw.toString().slice(0, 100))
965+
}
966+
}
967+
968+
// Handle binary frames (audio streaming)
950969
// Track activity for inactivity timeout
951970
if (agentId) {
952971
const session = agentSessions.get(agentId)
@@ -1544,15 +1563,16 @@ wss.on('connection', (ws) => {
15441563
}
15451564
})
15461565

1547-
ws.on('close', () => {
1566+
ws.on('close', (code, reason) => {
1567+
console.log('WS closed - agentId:', agentId, 'code:', code, 'reason:', reason?.toString())
15481568
if (agentId) {
15491569
destroySession(agentId)
15501570
agentId = null
15511571
}
15521572
})
15531573

15541574
ws.on('error', (err) => {
1555-
console.error('WebSocket error:', err.message)
1575+
console.error('WS error - agentId:', agentId, 'error:', err.message)
15561576
if (agentId) {
15571577
destroySession(agentId)
15581578
agentId = null

0 commit comments

Comments
 (0)