-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
text.gateway.ts
87 lines (81 loc) · 1.97 KB
/
text.gateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer
} from '@nestjs/websockets'
import { Socket, Server } from 'socket.io'
// Service
import { TextService } from '../services'
@WebSocketGateway()
export class TextGateway {
constructor(private textService: TextService) {}
@WebSocketServer()
io: Server
@SubscribeMessage('text')
async handleText(client: Socket, data: { data: { id: string } }) {
try {
const response = await this.textService.getText(data.data.id)
client.emit('text/' + data.data.id, response)
} catch (error) {
console.error(error)
}
}
@SubscribeMessage('texts')
async handleTexts(client: Socket) {
try {
const response = await this.textService.getTexts()
client.emit('texts', response)
} catch (error) {
console.error(error)
}
}
@SubscribeMessage('followText')
async handleFollowText(
client: Socket,
data: { token: string; data: { id: string } }
) {
try {
const response = await this.textService.followText(
data.data.id,
data.token,
this.io
)
client.emit('followText', response)
} catch (error) {
console.error(error)
}
}
@SubscribeMessage('unFollowText')
async handleUnFollowText(
client: Socket,
data: { token: string; data: { id: string } }
) {
try {
const response = await this.textService.unFollowText(
data.data.id,
data.token,
this.io
)
client.emit('unFollowText', response)
} catch (error) {
console.error(error)
}
}
@SubscribeMessage('postText')
async handlePostText(
client: Socket,
data: { token: string; data: { name: string; description: string } }
) {
try {
const response = await this.textService.postText(
data.data.name,
data.data.description,
data.token,
this.io
)
client.emit('postText', response)
} catch (error) {
console.error(error)
}
}
}