-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
141 lines (96 loc) · 3.52 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let siofu = require("socketio-file-upload");
let ss = require("socket.io-stream");
let express = require('express');
let path = require("path");
let http = require('http');
let static = require('serve-static');
let fs = require('fs');
let mime = require("mime/lite");
let app = express();
app.use(express.urlencoded({ extended: true }))
app.use(express.json());
app.set('view engine','ejs');
app.use(express.static('public'));
app.use('/public', static(__dirname));
app.use(express.static(path.join(__dirname, "/files")));
var PORT = process.env.PORT||82;
var httpServer = http.createServer(app).listen(PORT, function() {
console.log('listening on *:82');
})
const io = require('socket.io')(httpServer, { cors: { origin: "*" } });
const {Users} = require('./utils/users');
const {generateMessage} = require('./utils/message');
let users = new Users();
//mongoose//
const mongoose = require('mongoose');
const Msg = require('./modules/messages');
const mongoDB = 'mongodb+srv://sjy4974:[email protected]/message-database?retryWrites=true&w=majority'
////////////
mongoose.connect(mongoDB, {useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true}).then(() => {
console.log('connected');
}).catch(err => console.log(err));
//전달 데이터 생성//
app.get('/', (req,res) => {
var query = req.query;
console.log(query);
res.render('chat', query);
});
app.get('/1', (req,res) => {
res.render('index');
});
io.on('connection', (socket) => {
console.log(" user connect");
socket.on('join', (data, callback) => {
// 전달할 데이터 생성하기.
//??
socket.join(data.studyNo);
users.removeUser(data.email);
users.addUser(socket.id, data.email, data.nickname, data.profile, data.studyNo);
io.to(data.studyNo).emit('updateUsersList', users.getUserList(data.studyNo));
socket.broadcast.to(data.studyNo).emit('newMessage', generateMessage('SERVER','Admin', data.nickname+" 님이 접속 하셨 습니다."));
callback();
});
ss(socket).on('upload', (stream,data) =>{
var fn = path.basename(data.name);
console.log(fn);
stream.pipe(fs.createWriteStream("public/files/"+fn));
});
socket.on("done", (data) => {
io.to(data.studyNo).emit('newMessage',{
socketId : socket.id,
email: data.email,
name: data.name,
filename: data.filename,
type: data.type
});
})
socket.on('createMessage', (data, callback) => {
console.log("createmessage data값");
console.log(data);
let user = users.getUser(socket.id);
const msg = new Msg({studyNo:user.room, email:data.email, message:data.text});
msg.save().then(()=>{
io.to(user.room).emit('newMessage', generateMessage(user.email, user.name, data.text));
});
callback('This is the server:');
});
socket.on('disconnect', () => {
let user = users.removeUser(socket.id);
if(user){
io.to(user.room).emit('updateUsersList', users.getUserList(user.room));
io.to(user.room).emit('newMessage', generateMessage('SERVER','Admin', `${user.name} 님이 채팅 접속종료 .`));
}
});
//파일업로드 //
// let uploader = new siofu();
// uploader.dir="upload";
// uploader.listen(socket);
// uploader.on("saved", (event) =>{
// console.log(event.file);
// });
// uploader.on("error", (event) =>{
// console.log("Error from uploader", event);
// });
});