forked from animo/animo-mediator
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: pallavicoder <pallavi.ghule@ayanworks.com>
- Loading branch information
1 parent
1a33da3
commit 40cac71
Showing
6 changed files
with
75 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,73 @@ | ||
import { Express } from 'express' | ||
import { SocketIdsManager } from './SocketIdManager' | ||
import { processInboundMessage } from './ProcessInboundMessage' | ||
import { Logger } from '../logger' | ||
import { Agent } from '@credo-ts/core' | ||
|
||
export function registerSocketDockRoutes( | ||
app: Express, | ||
logger: Logger, | ||
socketIdManager: SocketIdsManager, | ||
agent: Agent | ||
) { | ||
app.post('/connect', async (req, res) => { | ||
logger.info('httpInboundTransport.connect') | ||
const sendUrl = req.body.meta.send | ||
const connectionId = req.body.meta.connection_id | ||
|
||
const socketId = socketIdManager.getConnectionBySocketId(connectionId) | ||
if (!socketId) { | ||
socketIdManager.addSocketId(connectionId) | ||
logger.debug(`Saving new socketId : ${connectionId}`) | ||
} | ||
|
||
if (!sendUrl) { | ||
logger.error('Missing "send" URL in request body') | ||
return res.status(400).send('Missing "send" URL') | ||
} | ||
|
||
try { | ||
res.status(200).send(`connection with socketId : ${connectionId} added successfully`) | ||
} catch (error) { | ||
res.status(500).send('Error sending response to send URL') | ||
} | ||
}) | ||
|
||
app.post('/message', async (req, res) => { | ||
logger.info('httpInboundTransport.message') | ||
|
||
const connectionId = req.body.meta.connection_id | ||
|
||
try { | ||
const socketId = socketIdManager.getConnectionBySocketId(connectionId) | ||
await processInboundMessage(req, res, agent, socketId) | ||
} catch (error) { | ||
res.status(500).send('Error sending response to send URL') | ||
} | ||
}) | ||
|
||
app.post('/disconnect', async (req, res) => { | ||
logger.info('httpInboundTransport.disconnect') | ||
const { connection_id } = req.body | ||
socketIdManager.removeSocketId(connection_id) | ||
logger.debug(`removed connection with socketId : ${connection_id}`) | ||
res.status(200).send(`connection with socketId : ${connection_id} removed successfully`) | ||
}) | ||
import { Agent, InboundTransport } from '@credo-ts/core' | ||
import { WebSocketTransportSession } from './SocketDockTransportSession' | ||
|
||
export class SocketDockInboundTransport implements InboundTransport { | ||
private app: Express | ||
private logger: Logger | ||
private agent: Agent | ||
private active_connections: Record<string, unknown> = {} | ||
|
||
constructor(app: Express, logger: Logger, agent: Agent) { | ||
this.app = app | ||
this.logger = logger | ||
this.agent = agent | ||
} | ||
|
||
async start(agent: Agent<any>): Promise<void> { | ||
this.app.post('/connect', async (req, res) => { | ||
this.logger.info('SocketDockInboundTransport.connect') | ||
const sendUrl = req.body.meta.send | ||
const connectionId = req.body.meta.connection_id | ||
|
||
const socketId = this.active_connections[connectionId] as string | ||
if (!socketId) { | ||
this.active_connections[socketId] = socketId | ||
this.logger.debug(`Saving new socketId : ${connectionId}`) | ||
} | ||
|
||
try { | ||
res.status(200).send(`connection with socketId : ${connectionId} added successfully`) | ||
} catch (error) { | ||
res.status(500).send('Error sending response to send URL') | ||
} | ||
}) | ||
|
||
this.app.post('/message', async (req, res) => { | ||
this.logger.info('SocketDockInboundTransport.message') | ||
|
||
const connectionId = req.body.meta.connection_id | ||
|
||
try { | ||
const socketId = this.active_connections[connectionId] as string | ||
const sendUrl = req.body.meta.send | ||
const requestMimeType = req.headers['content-type'] | ||
const session = new WebSocketTransportSession(socketId, res, sendUrl, requestMimeType) | ||
const message = req.body.message | ||
const encryptedMessage = JSON.parse(message) | ||
await agent.receiveMessage(encryptedMessage, session) | ||
if (!res.headersSent) { | ||
res.status(200).end() | ||
} | ||
} catch (error) { | ||
if (!res.headersSent) { | ||
res.status(500).send('Error processing message') | ||
} | ||
} | ||
}) | ||
|
||
this.app.post('/disconnect', async (req, res) => { | ||
this.logger.info('SocketDockInboundTransport.disconnect') | ||
const { connection_id } = req.body | ||
|
||
delete this.active_connections[connection_id] | ||
this.logger.debug(`removed connection with socketId : ${connection_id}`) | ||
res.status(200).send(`connection with socketId : ${connection_id} removed successfully`) | ||
}) | ||
} | ||
|
||
stop(): Promise<void> { | ||
throw new Error('Method not implemented.') | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.