Skip to content

Commit 0cea6af

Browse files
committed
add socket
1 parent 450b6fa commit 0cea6af

File tree

11 files changed

+782
-524
lines changed

11 files changed

+782
-524
lines changed

server/model.nlp

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

server/package-lock.json

Lines changed: 125 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"react": "^18.2.0",
6363
"reflect-metadata": "^0.1.13",
6464
"slugify": "^1.6.6",
65+
"socket.io": "^4.7.5",
6566
"swagger-jsdoc": "^6.2.1",
6667
"swagger-ui-express": "^4.5.0",
6768
"typedi": "^0.10.0",

server/src/app.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ import { LocaleService } from './i18n/ctx'
1818
import { getPreferredLocale } from './i18n/get-preferred-locale'
1919
import { loadAllLocales } from './i18n/i18n-util.sync'
2020

21+
// socket.io
22+
import { createServer } from 'http'
23+
import { Server } from 'socket.io'
24+
import { SocketController } from './controllers/socket.controller'
25+
2126
Container.set(LOCALE_KEY, new LocaleService('en'))
2227

2328
export class App {
2429
public app: express.Application
2530
public env: string
2631
public port: string | number
32+
public static io: Server
33+
2734

2835
constructor(routes: Routes[]) {
2936
this.app = express()
@@ -37,12 +44,28 @@ export class App {
3744
}
3845

3946
public listen() {
40-
this.app.listen(this.port, () => {
41-
logger.info(`=================================`)
42-
logger.info(`======= ENV: ${this.env} =======`)
43-
logger.info(`🚀 App listening on the port ${this.port}`)
44-
logger.info(`=================================`)
45-
})
47+
try {
48+
const socketController = new SocketController();
49+
const server = createServer(this.app);
50+
App.io = new Server(server);
51+
52+
App.io.on('connection', (socket) => {
53+
const query = socket.handshake.query;
54+
const userId = query.userId;
55+
socket.join(userId);
56+
socketController.handleSocketEvents(socket);
57+
});
58+
59+
server.listen(this.port, () => {
60+
logger.info(`=================================`)
61+
logger.info(`======= ENV: ${this.env} =======`)
62+
logger.info(`🚀 App listening on the port ${this.port}`)
63+
logger.info(`=================================`)
64+
})
65+
66+
} catch (error) {
67+
console.error('Error initializing server:', error);
68+
}
4669
}
4770

4871
public getServer() {

server/src/channels/line.channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class LineChannel extends BaseChannel {
9090
data: null,
9191
})
9292
}
93-
} catch (e) {}
93+
} catch (e) { }
9494
}
9595

9696
public async sendMessageToUser({ userId, text }) {

server/src/channels/web.channel.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { logger } from '@/utils/logger'
2+
import { Request, Response } from 'express'
3+
import { App } from '../app'
4+
import { BaseChannel } from './base.channel'
5+
6+
export class WebChannel extends BaseChannel {
7+
credentials: string
8+
9+
constructor(
10+
id: string,
11+
contactId: string,
12+
contactName: string,
13+
channelType: string,
14+
credentials: string,
15+
) {
16+
super(id, contactId, contactName, channelType)
17+
this.channelType = channelType;
18+
this.credentials = credentials;
19+
}
20+
21+
async prepareMessage(req: Request, res: Response) { }
22+
23+
public async sendMessageToUser({ userId, text }) {
24+
try {
25+
if (App.io) {
26+
App.io.to(userId).emit('message', { userId, message: text });
27+
}
28+
} catch (e) {
29+
logger.info(
30+
`[WEB] Bot send message to User failed - Error: ${e.message}`,
31+
)
32+
}
33+
}
34+
}

server/src/controllers/intent.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class IntentController {
2323
})
2424
})
2525

26+
// for bot
2627
public predictIntent = catchAsync(async (req: RequestWithUser, res) => {
2728
req.body.userId = req.user?.id
2829
const data = await this.intentService.PredictTrainIntent(req.body)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { LOCALE_KEY } from '@/constants'
2+
import { LocaleService } from '@/i18n/ctx'
3+
import { SocketService } from '@/services/socket.service'
4+
import { Socket } from 'socket.io'
5+
import Container from 'typedi'
6+
7+
export class SocketController {
8+
public socketService = Container.get(SocketService)
9+
public localeService = Container.get<LocaleService>(LOCALE_KEY)
10+
11+
public handleSocketEvents(socket: Socket) {
12+
this.socketService.handleSocketEvents(socket);
13+
}
14+
}

0 commit comments

Comments
 (0)