-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (64 loc) · 2.07 KB
/
app.js
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
const express = require('express');
const http = require('http');
const socket = require('socket.io');
const axios = require('axios');
const app = express();
const server = http.createServer(app);
const io = socket(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
const cors = require('cors')
app.use(cors());
const BASE_URL = "http://localhost:5000"
const MessageType = {
"message": "text",
"photo": "png",
"voice": "mp3"
};
const postMessageData = async (jsonData) => {
res = await axios.post(BASE_URL+'/chat/message', jsonData);
return res.data;
};
io.on('connection', socket => {
console.log('[LOG] Connect 이벤트');
socket.on('test', () => {
console.log('[LOG] test');
socket.emit('test', "test");
});
socket.on('joinRoom', (roomId) => {
socket.join(roomId);
console.log(`[LOG] Join 이벤트 : ${roomId}`);
})
socket.on('sendMessage', async (roomId, token, message) => {
console.log(`[LOG] ${roomId} 에서 메시지 이벤트 : ${message}`);
jsonData = {
"roomId": roomId,
"token": token,
"content": message,
"type": MessageType.message
};
messageData = await postMessageData(jsonData);
socket.broadcast.to(roomId).emit('realTimeChatting', messageData);
console.log(`[LOG] ${roomId} 에서 실시간 채팅 이벤트`);
console.log(messageData)
});
socket.on('sendImage', async (roomId, token, file) => {
console.log(`[LOG] ${roomId} 방에서 이미지 전송됨`);
jsonData = {
"roomId": roomId,
"token": token,
"content": file,
"type": MessageType.photo
};
messageData = await postMessageData(jsonData);
socket.broadcast.to(roomId).emit('realTimeChatting', messageData);
console.log(`[LOG] ${roomId} 에서 실시간 채팅 이벤트`);
console.log(messageData)
});
});
server.listen(3003, () => {
console.log('server on 3003')
});