-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
144 lines (115 loc) · 3.92 KB
/
index.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
140
141
142
143
144
'use strict';
//Loading dependencies & initializing express
var os = require('os');
var express = require('express');
var app = express();
var http = require('http');
//For signalling in WebRTC
var socketIO = require('socket.io');
const mysql=require('mysql')
const bodyParser = require('body-parser')
var session = require('express-session')
var MySQLStore = require('express-mysql-session')(session)
const router = require('./router/index')
const upload = require('./config/multer');
var ios = require("express-socket.io-session");
var options = {
host:'localhost',
user:'root',
password:'2207',
database:'user'
}
var sessionStore = new MySQLStore(options)
var session = session({
secret:"asdfasffdas",
resave:false,
saveUninitialized:true
});
app.set('views','./views')
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({extended:false}));
app.use(session)
app.use(router)
app.use(express.static('public'))
var server = http.createServer(app);
server.listen(process.env.PORT || 8080);
/*
app.get("/", function(req, res){
res.render("index.ejs");
});
*/
var io = socketIO(server);
//세션과 소켓 연결
io.use(ios(session,{ autoSave:true }));
io.sockets.on('connection', function(socket) {
// Convenience function to log server messages on the client.
// Arguments is an array like object which contains all the arguments of log().
// To push all the arguments of log() in array, we have to use apply().
function log() {
var array = ['Message from server:'];
array.push.apply(array, arguments);
socket.emit('log', array);
}
// 새로운 유저가 접속했을 경우 다른 소켓에게도 알려줌
socket.on('newUser', function(name) {
socket.otherName = socket.handshake.session.ID;
io.sockets.emit('update', {type: 'connect', otherName: socket.otherName, message: socket.otherName+'님이 접속하였습니다.'})
})
// 전송한 메시지 받기
socket.on('chat message', function(data) {
// 받은 데이터에 누가 보냈는지 이름을 추가
data.otherName = socket.otherName;
console.log(data.otherName+":"+data.message);
// 보낸 사람을 제외한 나머지 유저에게 메시지 전송
socket.broadcast.emit('update', data);
})
socket.on('image', (data)=>{
//이거 room 지속되는지 확인해봐야 함.
console.log(data);
data.otherName = socket.otherName;
//room별로 구별해서 채팅 보내기
//io.sockets.in(room).emit('image', data);
socket.broadcast.emit('imageupdate', data);
socket.emit('image', data);
});
//Defining Socket Connections
socket.on('message', function(message, room) {
log('Client said: ', message);
// for a real app, would be room-only (not broadcast)
socket.in(room).emit('message', message, room);
});
socket.on('create or join', function(room) {
log('Received request to create or join room ' + room);
var clientsInRoom = io.sockets.adapter.rooms[room];
var numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
log('Room ' + room + ' now has ' + numClients + ' client(s)');
if (numClients === 0) {
socket.join(room);
log('Client ID ' + socket.id + ' created room ' + room);
socket.emit('created', room, socket.id);
} else if (numClients === 1) {
log('Client ID ' + socket.id + ' joined room ' + room);
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room, socket.id);
io.sockets.in(room).emit('ready');
} else {
// max two clients
//추후 1:N 채팅구현시 변경
socket.emit('full', room);
}
});
socket.on('ipaddr', function() {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('ipaddr', details.address);
}
});
}
});
socket.on('bye', function(){
console.log('received bye');
});
});