Skip to content

Commit 879bf42

Browse files
committed
Set integration with Soket.io
1 parent d151955 commit 879bf42

9 files changed

Lines changed: 407 additions & 19 deletions

File tree

package-lock.json

Lines changed: 350 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"dependencies": {
4646
"express": "^4.18.1",
47-
"rxjs": "^7.5.5"
47+
"rxjs": "^7.5.5",
48+
"socket.io": "^4.5.1"
4849
}
4950
}

src/adapter/express.adpter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { NextFunction, Request, Response } from "express";
22

33
export default class ExpressAdapter {
4-
static create (fn) {
4+
static create (fn, io?) {
55
return async function (req: Request, res: Response, next: NextFunction) {
66
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
77
res.header("Pragma", "no-cache");
88

9-
const obj = await fn(req.params, req.body, res);
9+
const obj = await fn(req.params, req.body, res, io);
1010
return obj
1111
}
1212
}

src/controller/order/order.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import OrderRepositoryMemory from "@infra/repository/order/order-repository.memo
33
import GetOrderUseCase from "@usecase/order/get-order.usecase";
44

55
export default class OrderController {
6-
static getOrder(params, body, res) {
6+
static getOrder(params, body, res, io?) {
77
let rtn;
88
const orderMemory = new OrderRepositoryMemory();
99
const getOrder = new GetOrderUseCase(orderMemory);
1010
const order = getOrder.execute(params.id);
11+
1112
order.subscribe(p => {
1213
rtn = p;
1314
}).unsubscribe();
1415

1516
if (!rtn) return BaseData.sendResponse(404, 'nao retornou resultados', [], res);
17+
18+
//Dispara evento via Socket.io
19+
io.emit('test-message', 'Menssagem eviada via Socket.io para fins de teste');
1620
return BaseData.sendResponse(200, 'sucesso', Array(rtn), res);
1721
}
1822
}

src/infra/http/express.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,51 @@ import express from 'express';
33
import VerifyParamMiddleware from './middleware/validate-param.middleware';
44
import OrderController from '@controller/order/order.controller';
55
import RouteBaseBlock from '@infra/http/route/block-baseurl.route';
6+
import { createServer, Server } from 'http';
7+
import SocketIo from '@infra/http/socket.io';
68

79
export default class App {
810

911
private app: express.Application;
12+
private httpServer: Server;
13+
private io;
1014

11-
constructor(){
15+
constructor() {
1216
this.app = express();
17+
this.httpServer = createServer(this.app);
18+
this.io = SocketIo.serverIo(this.httpServer);
19+
20+
this.io.on("connection", (socket) => {
21+
console.log('Usuário conectado');
22+
23+
socket.on('disconnect', () => {
24+
console.log('usário desconectado');
25+
});
26+
});
1327
}
14-
28+
1529
//Root routes
16-
blockRootRoutes(){
30+
blockRootRoutes() {
1731
this.app.get(['/', '/teste'], ExpressAdapter.create(RouteBaseBlock.blockUrlBase));
1832
}
1933

2034
// Order API
21-
orderRoutes(){
22-
this.app.get('/teste/:id', VerifyParamMiddleware.checkParam, ExpressAdapter.create(OrderController.getOrder));
35+
orderRoutes() {
36+
this.app.get('/teste/:id', VerifyParamMiddleware.checkParam, ExpressAdapter.create(OrderController.getOrder, this.io));
2337
}
2438

25-
listen(port: number){
39+
listen(port: number, socket: boolean) {
40+
41+
// Para iniciar o servidor com Socket.io
42+
if (socket) {
43+
this.httpServer.listen(port, () => {
44+
console.log(`Serve is up in port ${port} with SocketI.io`);
45+
});
46+
return;
47+
}
48+
2649
this.app.listen(port, () => {
27-
console.log(`Serve is up in port ${port}`);
50+
console.log(`Serve is up in port ${port}, sem Socket.io`);
2851
});
2952
}
3053
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BaseData } from "@entity/baseData/base-data.entity";
22

33
export default class RouteBaseBlock {
4-
static blockUrlBase(params, body, res) {
4+
static blockUrlBase(params, body, res, io?) {
55
return BaseData.sendResponse(403, 'Url base com acesso restrito', [], res);
66
}
77
}

src/infra/http/socket.io.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Server } from "socket.io";
2+
3+
export default class SocketIo {
4+
static serverIo(http): Server {
5+
return new Server(http, {
6+
cors: {
7+
origin: '*',
8+
credentials: true
9+
}
10+
});
11+
}
12+
}

src/infra/repository/order/order-repository.memory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export default class OrderRepositoryMemory implements OrderRepository{
66

77
product = [{nome: "Biscoito triunfo", valor: 10.00}];
88
pedidos = [
9-
new OrderEntity(200, new Date("July 21, 1983 01:15:00"), this.product, 'aberto')
9+
new OrderEntity(200, new Date("July 21, 1983 01:15:00"), this.product, 'aberto'),
10+
new OrderEntity(100, new Date("July 21, 1983 01:15:00"), this.product, 'fechado'),
11+
new OrderEntity(14, new Date("July 21, 1983 01:15:00"), this.product, 'aberto')
1012
];
1113

1214
getOrder(id: number): Observable<OrderEntity> {

src/serve.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import App from "@infra/http/express";
33
const api = new App();
44

55
api.blockRootRoutes();
6+
67
api.orderRoutes();
78

8-
api.listen(3000);
9+
api.listen(3000, true);

0 commit comments

Comments
 (0)