-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.ts
More file actions
160 lines (135 loc) · 4.43 KB
/
socket.ts
File metadata and controls
160 lines (135 loc) · 4.43 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { FastifyPluginAsync } from 'fastify';
import fastifyIO from 'fastify-socket.io';
import { Lottie } from '../models/lottie';
import { Server } from 'socket.io';
import {
deleteLottieLayerProperty,
updateLottieColorProperty,
updateLottieSpeedProperty,
} from '../utils/lottie';
import {
CreateLottieMessage,
LottieAnimation,
LottieSocketEvents,
SocketAcknowledgement,
UpdateLottieMessage,
} from '../graphql/generated';
declare module 'fastify' {
interface FastifyInstance {
io: Server;
}
}
export const fastifySocketIo: FastifyPluginAsync = async (fastify) => {
await fastify.register(fastifyIO);
fastify.io.on('connection', async (socket) => {
const uuid = socket.handshake.query.uuid as string;
if (uuid && !socket.rooms.has(uuid)) {
fastify.log.info(`Client(${socket.id}):: Joining Room - ${uuid}`);
await socket.join(uuid);
fastify.log.info(`Client(${socket.id}):: Joined Room - ${uuid}`);
}
// This method is deprecated, use createLottie from GraphQL instead
socket.on(
LottieSocketEvents.CreateJson,
async (
message: CreateLottieMessage,
acknowledgement: (response: SocketAcknowledgement) => void,
) => {
fastify.log.info('Creating:: JSON lottie');
try {
await Lottie.create({
createdAt: new Date(),
updatedAt: new Date(),
uuid: message.uuid,
json: message.payload?.json,
});
fastify.log.info('Created:: JSON lottie');
acknowledgement({
code: 200,
status: 'ok',
});
} catch {
fastify.log.error('Error:: JSON lottie');
acknowledgement({
code: 500,
status: 'Could not create new JSON',
});
}
},
);
socket.on(
LottieSocketEvents.UpdateJson,
async (
message: UpdateLottieMessage,
acknowledgement: (response: SocketAcknowledgement) => void,
) => {
fastify.log.info(`Updating(${message.uuid}):: JSON lottie`);
const foundLottie = await Lottie.findOne({ uuid: message.uuid });
if (!foundLottie) {
fastify.log.error(`Updating(${message.uuid}):: JSON lottie not found`);
acknowledgement({
code: 404,
status: 'Could not find JSON',
});
return;
}
try {
let updatedLottie = undefined;
switch (message.payload.__typename) {
case 'ColorPayload':
updatedLottie = updateLottieColorProperty(
fastify,
foundLottie.json as LottieAnimation,
message.payload.layer,
message.payload.shape,
message.payload.shapeItem,
message.payload.color,
);
fastify.log.info(`Updating(${message.uuid}):: JSON lottie color`);
break;
case 'SpeedPayload':
updatedLottie = updateLottieSpeedProperty(
foundLottie.json as LottieAnimation,
message.payload.frameRate,
);
fastify.log.info(`Updating(${message.uuid}):: JSON lottie speed`);
break;
case 'LayerPayload':
updatedLottie = deleteLottieLayerProperty(
fastify,
foundLottie.json as LottieAnimation,
message.payload.layer,
);
fastify.log.info(`Updating(${message.uuid}):: JSON lottie layer deleted`);
break;
}
await Lottie.updateOne(
{ uuid: message.uuid },
{
$set: {
updatedAt: new Date(),
json: updatedLottie,
},
},
);
// Broadcast this change to every client in the room including the sender
fastify.io.to(message.uuid).emit(LottieSocketEvents.UpdateJson, message);
fastify.log.info(`Updating(${message.uuid}):: JSON lottie successfully updated`);
acknowledgement({
code: 200,
status: 'ok',
});
} catch {
fastify.log.error(`Updating(${message.uuid}):: Internal Server Error`);
acknowledgement({
code: 500,
status: 'Could not update JSON',
});
}
},
);
});
fastify.addHook('onReady', () => {
fastify.log.info('WebSocket server ready');
});
};