Showing
1 changed file
with
71 additions
and
0 deletions
lib/socketio.js
0 → 100644
| 1 | +const passport_IO = require('./passport+socketio.js'), | ||
| 2 | + passportSocketIo = require("passport.socketio"), | ||
| 3 | + db = require('../lib/db.js'); | ||
| 4 | + | ||
| 5 | +module.exports = (server, app) => { | ||
| 6 | + | ||
| 7 | + const io = require('socket.io', )(server, { | ||
| 8 | + transports: ['websocket'] | ||
| 9 | + }); | ||
| 10 | + | ||
| 11 | + io.use(passportSocketIo.authorize(passport_IO)); //passport와 socketIO 연동 미들웨어 사용 | ||
| 12 | + app.set('io', io); //io를 app.get("io")로 다른 라우터에서 접근 가능하게함 | ||
| 13 | + const chat = io.of('/chat'); //chat namespace | ||
| 14 | + | ||
| 15 | + chat.on('connection', (socket) => { //네임스페이스 연결시 루프 동작 | ||
| 16 | + let room; | ||
| 17 | + socket.to(room).emit('chat_sended_to_client', "LALALALALAL"); | ||
| 18 | + socket.on('disconnecting', (reason) => { | ||
| 19 | + | ||
| 20 | + const sql = "DELETE FROM participants WHERE id=?" | ||
| 21 | + console.log(socket.request.user.nickname, room, '번방 퇴장'); | ||
| 22 | + db.query(sql, [socket.request.user.id], (err, result) => { //퇴장할때 참가자 목록에서 뺌 | ||
| 23 | + socket.leave(room); | ||
| 24 | + | ||
| 25 | + const msg = {}; | ||
| 26 | + const user=socket.request.user; | ||
| 27 | + msg.time = socket.handshake.time.slice(0, 24); | ||
| 28 | + msg.sended = user.id; | ||
| 29 | + msg.sended_Nickname = user.nickname; | ||
| 30 | + msg.profile_image=user.profile_image; | ||
| 31 | + msg.description = `${msg.sended_Nickname}님이 퇴장하셨습니다.`; | ||
| 32 | + | ||
| 33 | + socket.to(room).emit("other_leaved_room", msg); | ||
| 34 | + room = 0; | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + socket.on("connection", (roomnum) => { //방 접속시에 현재 방번호 room에 저장 | ||
| 38 | + room = roomnum; | ||
| 39 | + console.log(socket.request.user.nickname, room, '번방 입장'); | ||
| 40 | + socket.join(room); // x번 room에 join시킴 | ||
| 41 | + const msg = {}; | ||
| 42 | + const user= socket.request.user; | ||
| 43 | + msg.time = socket.handshake.time.slice(0, 24); | ||
| 44 | + msg.sended = `${user.id}`; | ||
| 45 | + msg.sended_Nickname = user.nickname; | ||
| 46 | + msg.profile_image = user.profile_image; | ||
| 47 | + msg.description = `${user.nickname}님이 들어오셨습니다.` | ||
| 48 | + | ||
| 49 | + const sql = 'INSERT INTO participants (room,id,name,nickname,profile_image) VALUES (?,?,?,?,?)'; //add user in room | ||
| 50 | + db.query(sql, [roomnum, user.id, user.name, user.nickname, user.profile_image], (err, result) => { | ||
| 51 | + chat.to(room).emit("new_user_in", msg); //방에 속한 모든 사람들에게 내가 들어왔음을 알림 | ||
| 52 | + }) | ||
| 53 | + }) | ||
| 54 | + socket.on('chat_sended_to_server', (data) => { // | ||
| 55 | + console.log('chat received', socket.request.user.id, data); | ||
| 56 | + const msg = {}; | ||
| 57 | + msg.time = socket.handshake.time.slice(0, 24); | ||
| 58 | + msg.sended = `${socket.request.user.id}`; | ||
| 59 | + msg.sended_Nickname = socket.request.user.nickname; | ||
| 60 | + msg.profile_image = socket.request.user.profile_image; | ||
| 61 | + msg.description = data; | ||
| 62 | + console.log(room); | ||
| 63 | + const sql = 'INSERT INTO chat (room, description,sended,sended_nickname,time,profile_image) VALUES (?,?,?,?,?,?)'; | ||
| 64 | + db.query(sql, [room, msg.description, msg.sended, msg.sended_Nickname, msg.time, msg.profile_image]); //채팅한 말 객체들의 배열 [ { room : 10 "sended":"YOUT","sended_NickName":"YOU" , time : "now" , description : "lala", profile_image : "!@#@!#"} , ... ] | ||
| 65 | + chat.to(room).emit('chat_sended_to_client', msg); | ||
| 66 | + }); | ||
| 67 | + }) | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +} |
-
Please register or login to post a comment