forked from yutaka-m/node-webrtc-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (67 loc) · 2.01 KB
/
app.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
var app = require('express').createServer();
var io = require('socket.io').listen(app);
app.get('/',function(req,res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/md5.min.js',function(req,res) {
res.sendfile(__dirname + '/md5.min.js');
});
var rooms = new Array();
var chat = io.of('/chat').on('connection', function(socket) {
// Room予約
// 戻り値
// 0 : Room使用可能通知(正常:2回目)
// 1 : Room予約完了通知(正常:1回目)
// 2 : 既に使用済み
// 3 : 既に予約済み
socket.on('room', function(data) {
console.log('room');
var json = JSON.parse(data);
var member = rooms[json.name];
var result = -1;
if (undefined == member) {
member = new Array();
}
if (member.length > 2) {
result = 2;
chat.sockets[socket.id].emit('resultRoom',JSON.stringify(result));
return;
}
if (member.indexOf(socket.id) != -1) {
result = 3;
chat.sockets[socket.id].emit('resultRoom',JSON.stringify(result));
return;
} else {
member.push(socket.id);
rooms[json.name] = member;
if (member.length == 2) {
result = 0;
if (member[0] in chat.sockets)
chat.sockets[member[0]].emit('resultRoom',JSON.stringify(result));
} else {
result = 1;
chat.sockets[socket.id].emit('resultRoom',JSON.stringify(result));
}
return;
}
});
// SDPメッセージ通信
socket.on('sdp', function(data) {
console.log('sdp');
var json = JSON.parse(data);
var member = rooms[json.name];
if (undefined == member) { console.log('no member'); return; }
for(var i=0;i < member.length; i++) {
if (member[i] != socket.id) {
if (member[i] in chat.sockets)
chat.sockets[member[i]].emit('resultSDP',JSON.stringify(json.sdp));
}
}
});
socket.on('disconnect', function () {
console.log('disconnect');
// clear room
});
});
var port = process.env.PORT || 3001;
app.listen(port);