server.js 1.42 KB
Newer Older
우지원's avatar
우지원 committed
1
2
3
import express from 'express'
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"
11

우지원's avatar
우지원 committed
12
connectDb()
Choi Ga Young's avatar
Choi Ga Young committed
13

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

16
17
18
19
20
21
22
const server = http.createServer(app);

const io = new Server(server)

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

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
29
  socket.on('chat', (data) => {
30
    console.log('roomname확인', data)
JeongYeonwoo's avatar
JeongYeonwoo committed
31
    socket.broadcast.to(data.roomInfo).emit('sendedMSG', data.sendInfo ); // sender 제외 특정 방으로  
Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
  });
33

JeongYeonwoo's avatar
JeongYeonwoo committed
34

35
36
37
  socket.on('disconnect', () => {
    console.log('disconnected from server id=', socket.id)
  })
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
38
  
39
40
41
});

app.use(bodyParser.json());
42
app.use(cors());
43

JeongYeonwoo's avatar
JeongYeonwoo committed
44
app.use('/images', express.static('uploads/'))
우지원's avatar
우지원 committed
45
app.use(userRouter)
46
app.use(authRouter)
47
app.use(roomRouter)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
48
app.use(roomEnterRouter)
Choi Ga Young's avatar
Choi Ga Young committed
49

JeongYeonwoo's avatar
JeongYeonwoo committed
50
server.listen(3030, () => {
51
  console.log('Listening on port 3030')
52
})
53

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