Skip to content

Commit

Permalink
removed transport session if socket is disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
RinkalBhojani committed Dec 17, 2024
1 parent becbc31 commit e509b2a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/transport/SocketDockInboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Express } from 'express'
import { Agent, AgentEventTypes, AgentMessageReceivedEvent, InboundTransport } from '@credo-ts/core'
import { Agent, AgentEventTypes, AgentMessageReceivedEvent, InboundTransport, TransportService } from '@credo-ts/core'
import { SocketDockTransportSession } from './SocketDockTransportSession'
import express from 'express'

Expand All @@ -23,7 +23,7 @@ export class SocketDockInboundTransport implements InboundTransport {

const socketId = this.activeConnections[connectionId]
if (!socketId) {
this.activeConnections[socketId] = socketId
this.activeConnections[connectionId] = connectionId
agent.config.logger.debug(`Saving new socketId : ${connectionId}`)
}

Expand All @@ -44,12 +44,15 @@ export class SocketDockInboundTransport implements InboundTransport {

try {
const socketId = this.activeConnections[connectionId]
agent.config.logger.debug(`activeConnections transport session : ${ socketId} ${connectionId}`)
const sendUrl = req.body.meta.send
const requestMimeType = req.headers['content-type'] as string
const session = new SocketDockTransportSession(socketId, res, sendUrl, requestMimeType)
const message = req.body.message
const encryptedMessage = JSON.parse(message)

agent.config.logger.debug(`Session value for transport session : ${ session.id}`)

agent.events.emit<AgentMessageReceivedEvent>(agent.context, {
type: AgentEventTypes.AgentMessageReceived,
payload: {
Expand All @@ -66,12 +69,19 @@ export class SocketDockInboundTransport implements InboundTransport {

this.app.post('/disconnect', async (req, res) => {
agent.config.logger.info('SocketDockInboundTransport.disconnect')
const { connection_id } = req.body
const transportService = agent.dependencyManager.resolve(TransportService)
const { connection_id } = req.body
if (!connection_id) {
throw new Error('ConnectionId is not sent from socketDock server')
}

delete this.activeConnections[connection_id]
const session = transportService.findSessionById(connection_id)
agent.config.logger.debug(`Got session from transportService ${session?.id}`)
if(session) {
transportService.removeSession(session)
}

agent.config.logger.debug(`removed connection with socketId : ${connection_id}`)
res.status(200).send(`connection with socketId : ${connection_id} removed successfully`)
})
Expand Down

0 comments on commit e509b2a

Please sign in to comment.