server.js 2.47 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
14
import User from "./models/User.js"
import AccessInfo from "./models/AccessInfo.js"
15

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

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

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

Choi Ga Young's avatar
x    
Choi Ga Young committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  // socket.on('access', async (data) => {
  //   console.log('access', data)
  //   const nick = await User.findOne({ _id: data.userInfo }).select('nickname').exec()
  //   const room = await Room.findOne({ roomId: data.rmIf })
  //   console.log('room확인2', room)
  //   //console.log('nick확인', nick) //{ _id: 5ffe897927a051f4bc17bbcc, nickname: 'rkyoung1' }
  //   const accessInfo = await new AccessInfo({
  //     room: room._id,
  //     userInfo: data.userInfo,
  //     nickname: nick.nickname,
  //     isEnt: true
  //   }).save()
  //   console.log('acc확인', accessInfo)
  // })
Choi Ga Young's avatar
Choi Ga Young committed
47

Choi Ga Young's avatar
Choi Ga Young committed
48
  socket.on('chat', async (data) => {
49
    console.log('roomname확인', data)
Choi Ga Young's avatar
Choi Ga Young committed
50
51
52
53
54
    const room = await Room.findOne({ roomId: data.roomInfo })
    console.log('room이 떴나', room)

    const chat = await new Chat({
      room: room._id,
55
56
      username: data.sendInfo.sender,
      message: data.sendInfo.msg
Choi Ga Young's avatar
Choi Ga Young committed
57
58
59
    }).save()
    console.log('resChat확인', chat)

Choi Ga Young's avatar
Choi Ga Young committed
60
    socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.sendInfo); // sender 제외 특정 방으로
61
    console.log('broad cst실핼')
Choi Ga Young's avatar
Choi Ga Young committed
62
63
64
65
66
  });

  socket.on('disconnect', () => {
    console.log('disconnected from server id=', socket.id)
  })
67
68
69
70

});

app.use(bodyParser.json());
71
app.use(cors());
72

JeongYeonwoo's avatar
JeongYeonwoo committed
73
app.use('/images', express.static('uploads/'))
우지원's avatar
우지원 committed
74
app.use(userRouter)
75
app.use(authRouter)
76
app.use(roomRouter)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
77
app.use(roomEnterRouter)
Choi Ga Young's avatar
Choi Ga Young committed
78

JeongYeonwoo's avatar
JeongYeonwoo committed
79
server.listen(3030, () => {
80
  console.log('Listening on port 3030')
81
})
82

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