server.js 1.81 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"
Choi Ga Young's avatar
x    
Choi Ga Young committed
13
import User from "./models/User.js"
14

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

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

19
20
21
22
23
24
25
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
26
27
28
  socket.on('joinRoom', (data) => { // joinRoom을 클라이언트가 emit했을 때
    console.log('join_data확인', data)
    let roomInfo = data;
29
    socket.join(roomInfo); //클라이언트에서 data에 적힌 room으로 참여시킴
30
31
  });

Choi Ga Young's avatar
Choi Ga Young committed
32
  socket.on('chat', async (data) => {
33
    console.log('roomname확인', data)
Choi Ga Young's avatar
Choi Ga Young committed
34
35
36
37
38
    const room = await Room.findOne({ roomId: data.roomInfo })
    console.log('room이 떴나', room)

    const chat = await new Chat({
      room: room._id,
39
      username: data.sendInfo.sender,
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
40
      message: data.sendInfo.msg 
Choi Ga Young's avatar
Choi Ga Young committed
41
42
43
    }).save()
    console.log('resChat확인', chat)

Choi Ga Young's avatar
Choi Ga Young committed
44
    socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.sendInfo); // sender 제외 특정 방으로
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
45
46

  }); 
Choi Ga Young's avatar
Choi Ga Young committed
47
48
49
50

  socket.on('disconnect', () => {
    console.log('disconnected from server id=', socket.id)
  })
51
52
53
54

});

app.use(bodyParser.json());
55
app.use(cors());
56

JeongYeonwoo's avatar
JeongYeonwoo committed
57
app.use('/images', express.static('uploads/'))
우지원's avatar
우지원 committed
58
app.use(userRouter)
59
app.use(authRouter)
60
app.use(roomRouter)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
61
app.use(roomEnterRouter)
Choi Ga Young's avatar
Choi Ga Young committed
62

JeongYeonwoo's avatar
JeongYeonwoo committed
63
server.listen(3030, () => {
64
  console.log('Listening on port 3030')
65
})
66

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