-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (117 loc) · 3.03 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
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
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(8080);
var count = 0;
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var len = 200;
var users = new Array(len);
var rooms = new Array(len);
for(var c=0; c < len; c++)
{
users[c] = 0;
rooms[c] = 0;
}
io.sockets.on('connection', function (socket) {
socket.on('stopchat', function(){
if( socket.room != "null")
{ io.sockets.in(socket.room).emit('updatechat', 'server', 'disconnect'); }
var r = socket.roomindex;
io.sockets.clients(socket.room).forEach(function(s) {
s.stat = "idle";
s.leave(s.room);
s.room = "null";
s.roomindex = "null";
});
socket.leave(r);
rooms[r] = 0;
});
socket.on('counter', function(){
socket.emit('updatechat', 'server', count);
});
socket.on('adduser', function(){
var i = 0;
for(i = 0; i < users.length; i++)
{
if( users[i] != 1)
{ socket.username = i;
users[i] = 1;
break;
}
}
socket.emit('updatechat', 'server', count);
socket.emit('updatechat', 'username', i);
socket.stat = "idle";
socket.room = "null";
count++;
});
socket.on('newconn', function(){
var i=0;
socket.emit('updatechat', 'server', count);
var freearr = new Array(len);
var c = 0;
for(i=0; i < rooms.length; i++)
{
if( rooms[i] == 1 )
{
freearr[c] = i;
c++;
}
}
if( c > 0 )
{
var a = Math.floor( Math.random() * (c - 0) + 0 );
i = freearr[a];
rooms[i] = 2;
socket.room = i.toString();
socket.join(socket.room);
socket.roomindex = i;
socket.stat = "connected";
socket.broadcast.to(socket.room).emit('updatechat', 'server', 'connected');
socket.emit('updatechat', 'server', 'connected');
}
if( socket.stat != "connected" )
{
c = 0;
for(i=0; i < rooms.length; i++)
{
if( rooms[i] == 0 )
{
rooms[i] = 1;
socket.room = i.toString();
socket.join(socket.room);
socket.roomindex = i;
socket.stat = "waiting";
socket.emit('updatechat', 'server', socket.stat);
//socket.emit('updatechat', 'strange', socket.room);
break;
}
}
}
});
socket.on('sendchat', function (data) {
if(socket.room != "null")
{ io.sockets.in(socket.room).emit('updatechat', socket.username, data); }
});
socket.on('disconnect', function(){
if( socket.room != 'null' && socket.stat != 'waiting' )
{ io.sockets.in(socket.room).emit('updatechat', 'server', 'disconnect'); }
var r = socket.roomindesx;
io.sockets.clients(socket.room).forEach(function(s) {
s.stat = "idle";
s.leave(s.room);
s.room = "null";
});
socket.leave(r);
users[socket.username] = 0;
rooms[socket.roomindex] = 0;
delete users[socket.username];
users[socket.username] = 0;
if( count > 1 )
{ count--; }
});
});