server.js 2.3 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import express from 'express'
우지원's avatar
우지원 committed
2
3
import connectDb from './utils/connectDb.js'
import userRouter from './routes/user.routes.js'
4
import authRouter from './routes/auth.routes.js'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
5
import roomRouter from './routes/room.routers.js'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
6
import roomEnterRouter from './routes/roomEnter.routers.js'
7
8
9
import bodyParser from "body-parser";
import http from "http";
import { Server } from 'socket.io';
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
10
import cors from "cors"
Choi Ga Young's avatar
Choi Ga Young committed
11
12
import Room from './models/Room.js'
import Chat from "./models/Chat.js"
13

우지원's avatar
우지원 committed
14
connectDb()
Choi Ga Young's avatar
Choi Ga Young committed
15

우지원's avatar
우지원 committed
16
const app = express()
Choi Ga Young's avatar
Choi Ga Young committed
17

18
19
20
21
22
23
24
const server = http.createServer(app);

const io = new Server(server)

io.on("connection", (socket) => { // 기본 연결
  console.log("socket connect ok", socket.id)

Choi Ga Young's avatar
Choi Ga Young committed
25
  socket.on('joinRoom', (data) => { // joinRoom을 클라이언트가 emit했을 때
26
    // console.log('join_data확인', data)
Choi Ga Young's avatar
Choi Ga Young committed
27
    let roomInfo = data;
28
    socket.join(roomInfo); //클라이언트에서 data에 적힌 room으로 참여시킴
29
30
  });

Soo Hyun Kim's avatar
Soo Hyun Kim committed
31
  socket.on('closeRoom', (data) => {
32
    // console.log('close_data확인', data)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
33
34
35
36
    let roomInfo = data;
    socket.leave(roomInfo); //클라이언트에서 data에 적힌 room정보를 브로드캐스팅 받지 않는다.
  });

Choi Ga Young's avatar
Choi Ga Young committed
37
  socket.on('newUser', (data) => {
38
    // console.log('newUser', data)
Choi Ga Young's avatar
Choi Ga Young committed
39
40
41
42
    let userInfo = data.userInfo;
    io.to(data.rmIf).emit('sendUser', userInfo)
  })

Choi Ga Young's avatar
Choi Ga Young committed
43
  socket.on('chat', async (data) => {
44
    // console.log('roomname확인', data)
Choi Ga Young's avatar
Choi Ga Young committed
45
    const room = await Room.findOne({ roomId: data.roomInfo })
46
    // console.log('room이 떴나', room)
Choi Ga Young's avatar
Choi Ga Young committed
47
48
49

    const chat = await new Chat({
      room: room._id,
50
      username: data.sendInfo.sender,
51
52
      message: data.sendInfo.msg,
      profileimg: data.sendInfo.img
Choi Ga Young's avatar
Choi Ga Young committed
53
    }).save()
54
    // console.log('resChat확인', chat)
Choi Ga Young's avatar
Choi Ga Young committed
55

Choi Ga Young's avatar
Choi Ga Young committed
56
    socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.sendInfo); // sender 제외 특정 방으로
57
    socket.broadcast.emit('checking', data.sendInfo)  // 메세지를 어딘가에서 보냈다는 신호
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
58
  }); 
Choi Ga Young's avatar
Choi Ga Young committed
59
60
61
62

  socket.on('disconnect', () => {
    console.log('disconnected from server id=', socket.id)
  })
63
64
65
});

app.use(bodyParser.json());
66
app.use(cors());
67

JeongYeonwoo's avatar
JeongYeonwoo committed
68
app.use('/images', express.static('uploads/'))
우지원's avatar
우지원 committed
69
app.use(userRouter)
70
app.use(authRouter)
71
app.use(roomRouter)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
72
app.use(roomEnterRouter)
Choi Ga Young's avatar
Choi Ga Young committed
73

JeongYeonwoo's avatar
JeongYeonwoo committed
74
server.listen(3030, () => {
75
  console.log('Listening on port 3030')
76
})
77

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
78
export default server
79
// module.exports = server;