-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
230 lines (195 loc) · 7.36 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const fs = require('fs');
var use_require_json = require('./api_key.json');
var key = use_require_json.key;
var port = process.env.PORT || 80;
var express = require('express');
var app = express();
var server = app.listen(port);
app.use(express.static('./public'));
let broadcaster;
var socket = require('socket.io');
const options = {
serveClient: true
//pingTimeout: 25000,
//pingInterval: 5000,
//transports: ['polling']
//transports: ['websockets']
}
var io = socket(server, options);
function isExistFile(file) {
try {
fs.statSync(file);
return true
} catch (err) {
if (err.code === 'ENOENT') return false
}
}
function escapeForCSV(input) {
if (/[",\n\t]/.test(input)) { // タブもエスケープの対象に追加
input = '"' + input.replace(/"/g, '""') + '"';
}
return input;
}
io.on('connection', (socket) => {
console.log('connection', socket.id);
var room = "";
var room_master = "-master";
var flg_deactivate_comment_control = false;
var number_of_users = 0;
// 接続者に対してコネクションを作ったことを知らせるメッセージ
socket.emit('you_are_connected');
socket.on("join", (room_to_join) => {
if (room_to_join == "") room_to_join = "undefined-room"
socket.join(room_to_join);
console.log(socket.id, " joined to ", room_to_join);
room = room_to_join;
const filepath = "public/chatlogs/" + room + ".csv";
let timestamp;
let today = new Date();
if (isExistFile(filepath)) {
const stats = fs.statSync(filepath);
let d = new Date(stats.mtime);
let past_h = ((today - d) / (1000 * 60 * 60));
let past_s = ((today - d) / 1000);
// 最終更新から24時間経過してればファイル内容は削除
console.log("file timestamp: ", past_h);
if (past_h > 24) {
fs.unlinkSync(filepath);
console.log("24 h over: deleted ", filepath);
}
// console.log(past_h, past_s);
}
// no exit, let's create
else {
// const fs = require('fs');
// fs.writeFileSync(filepath, '');
// console.log('create: ', filepath);
}
// number_of_users = io.sockets.adapter.rooms[room].length;
number_of_users = io.sockets.adapter.rooms.get(room).size;
console.log(number_of_users)
// we store the username in the socket session for this client
socket.username = socket.id; //username;
socket.emit('login', {
numUsers: number_of_users,
deactivate_comment_control: io.sockets.adapter.rooms.get(room).flg_deactivate_comment_control
});
// echo globally (all clients) that a person has connected
socket.to(room).emit('user joined', {
username: socket.username,
numUsers: number_of_users
});
});
socket.on("join-as-master", (room_to_join) => {
if (room_to_join == "") room_to_join = "undefined-room"
socket.join(room_to_join);
//console.log(socket.id, "joined master to ", room_to_join);
room_master = room_to_join;
});
// when the client emits 'new message', this listens and executes
socket.on('new message', (data) => {
// we tell the client to execute 'new message'
socket.to(room).emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'new message', this listens and executes
socket.on('comment', (data) => {
// we tell the client to execute 'new message'
data.socketid = socket.id;
// 全員に送信
socket.to(room).emit('comment', data);
// ログを書き込む 現在は textableも使わないので、コメントアウトしちゃう
// const filepath = "public/chatlogs/" + room + ".csv";
// let timestamp;
// let today = new Date();
// timestamp = today;
// fs.appendFileSync(filepath, `${timestamp},${escapeForCSV(data.my_name)},${escapeForCSV(data.name_to)},${escapeForCSV(data.comment)},${data.id_comment},${data.flg_emoji},${data.flg_sound},${data.flg_speech},${socket.id}\n`);
// console.log(data.comment);
});
socket.on('delete comment', (data) => {
socket.to(room).emit('delete comment', data);
//console.log(data);
// data.id の当たるCSVの行を削除する
const filepath = "public/chatlogs/" + room + ".csv";
let records = [];
if (isExistFile(filepath)) {
const csvs = fs.readFileSync(filepath, 'utf-8');
let rows = csvs.split('\n');
for (row of rows) {
let cols = row.split(',');
if (cols.length == 5) {
if (cols[4] == data.id) {
}
else {
records.push(row);
}
}
}
fs.unlinkSync(filepath);
for (record of records) {
fs.appendFileSync(filepath, `${record}\n`);
}
}
else {
console.log("Error: no such file, ", filepath);
}
});
socket.on('stop streaming', () => {
socket.to(room_master).emit('stop streaming');
});
socket.on('telop', (data) => {
socket.to(room_master).emit('telop', data);
});
socket.on('letter', (data) => {
socket.to(room_master).emit('letter', data);
});
socket.on('display_clock', (data) => {
socket.to(room_master).emit('display_clock', data);
});
socket.on('glitch_effect', (data) => {
socket.to(room_master).emit('glitch_effect', data);
});
socket.on('toggleQR', (data) => {
socket.to(room_master).emit('toggleQR', data);
});
socket.on('deactivate_comment_control', (data) => {
if (room == "") {
// 部屋名が初期値のままの場合(join接続されていない)は無視
}
else {
flg_deactivate_comment_control = data.control;
socket.to(room).emit('deactivate_comment_control', data);
io.sockets.adapter.rooms.get(room).flg_deactivate_comment_control = flg_deactivate_comment_control;
}
});
socket.on("broadcaster", () => {
broadcaster = socket.id;
socket.broadcast.emit("broadcaster");
});
socket.on("watcher", () => {
socket.to(broadcaster).emit("watcher", socket.id, number_of_users);
});
socket.on("offer", (id, message) => {
socket.to(id).emit("offer", socket.id, message);
});
socket.on("answer", (id, message) => {
socket.to(id).emit("answer", socket.id, message);
});
socket.on("candidate", (id, message) => {
socket.to(id).emit("candidate", socket.id, message);
});
// when the user disconnects.. perform this
socket.on('disconnect', () => {
number_of_users--;
// echo globally that this client has left
socket.to(room).emit('user left', {
username: socket.username,
numUsers: number_of_users
});
socket.to(broadcaster).emit("disconnectPeer", socket.id, number_of_users);
socket.leave(room);
//console.log(socket.id, " has been leaved from ", room);
});
});