Skip to content

Commit f7d9c8d

Browse files
committed
Provide number of clients
1 parent 36f7365 commit f7d9c8d

File tree

3 files changed

+38
-46
lines changed

3 files changed

+38
-46
lines changed

routes/api/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,18 @@ function ServerObserver() {
134134
}
135135

136136
ServerObserver.prototype.loop = function() {
137-
137+
let self = this;
138138
for( srv in this.observers ) {
139139

140140
GetCachedStatus(srv,5001)
141141
.then( data => {
142+
var toSend = JSON.stringify({
143+
data: data,
144+
nrOfClients: self.getNrOfClients(),
145+
})
142146
this.observers[srv].forEach( ws => {
143147
try {
144-
ws.send( JSON.stringify(data) )
148+
ws.send( toSend )
145149
}
146150
catch( ex ) {
147151
this.unsubscribe( ws );
@@ -150,6 +154,7 @@ ServerObserver.prototype.loop = function() {
150154
})
151155
.catch( err => {
152156
console.log("Can't get cached status for ", srv, err );
157+
//TODO: check this context
153158
this.observers[srv].forEach( ws => {
154159
ws.close()
155160
})
@@ -161,14 +166,27 @@ ServerObserver.prototype.loop = function() {
161166

162167
}
163168

169+
ServerObserver.prototype.getNrOfClients = function(server) {
170+
let reply = 0
171+
for( var server in this.observers ) {
172+
reply += this.observers[server].length
173+
}
174+
return reply
175+
}
176+
164177
ServerObserver.prototype.subscribe = function(server,ws) {
178+
//console.log("subscribe",server,ws._socket.remoteAddress);
179+
let self = this;
165180
this.unsubscribe(ws);
166181
(this.observers[server] = (this.observers[server] || [])).push(ws);
167182
try {
168183
GetCachedStatus(server,5001)
169184
.then( data => {
170185
try {
171-
ws.send( JSON.stringify(data) )
186+
ws.send( JSON.stringify({
187+
data: data,
188+
nrOfClients: self.getNrOfClients(),
189+
}))
172190
}
173191
catch( ex ) {
174192
this.unsubscribe( ws );

static/mpmap.js

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -220,46 +220,15 @@ $(function() {
220220

221221
function haveData( data ) {
222222
setConnected(true);
223-
data.clients.sort(function(a,b) {
223+
data.data.clients.sort(function(a,b) {
224224
return a.callsign.localeCompare(b.callsign)
225225
})
226-
aircraftLayer.fire('mpdata',{ data: data },aircraftLayer)
227-
setPilotsList( data.clients )
226+
aircraftLayer.fire('mpdata',{ data: data.data },aircraftLayer)
227+
setPilotsList( data.data.clients )
228+
$("#nrOfClients").html(data.nrOfClients);
228229
}
229230

230-
var retryCnt = 3
231-
function loadData() {
232-
$.ajax( "api/stat/" + settings.server, {
233-
success: function(data) {
234-
haveData( data );
235-
if( settings.refresh > 0 ) {
236-
setTimeout( function() {
237-
loadData()
238-
},settings.refresh*1000)
239-
}
240-
},
241-
error: function(a,b,c) {
242-
setConnected(false);
243-
console.log("AJAX error, retry=", retryCnt,a,b,c)
244-
if( retryCnt > 0 ) {
245-
retryCnt--;
246-
if( settings.refresh > 0 ) {
247-
setTimeout( function() {
248-
loadData()
249-
},settings.refresh*1000)
250-
}
251-
} else {
252-
alert("Can't load multiplayer data, please reload page")
253-
}
254-
},
255-
timeout: 30000,
256-
})
257-
}
258-
259-
// loadData()
260-
261231
var ws = null;
262-
263232
function createWebsocket() {
264233
var wsUrl = (window.location.protocol === 'https' ? 'wss' : 'ws') +
265234
'://' + window.location.host + '/api/stream';

views/main.pug

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,30 @@ html(lang="en")
7979
span.fas.fa-info-circle
8080
span  About
8181
div#collapseThree.collapse(data-parent="#accordion",role="tabpanel",aria-labelledby="headingThree",aria-expanded="false")
82-
div.card-body
83-
p
82+
div.card-body
83+
p
8484
| This is <a href="http://www.flightgear.org">FlightGear</a> μ (mju) aka mpmap.js, a multiplayer map for based on node.js with
8585
| the <a href="https://github.com/t3r/mpmap.js">source code on github</a>.
8686
p
87-
| If you like this map and use it regularly, please consider donating
88-
| for the hosting and bandwidth costs. Donations also help operating the
87+
| If you like this map and use it regularly (it looks like
88+
|
89+
span.font-weight-bold#nrOfClients 0
90+
|
91+
| users enjoy it at the moment), please consider donating
92+
| for the hosting and bandwidth costs. Donations also help operating the
93+
|
8994
a(href="https://scenery.flightgear.org",target="_blank") FlightGear scenemodels database.
90-
|
91-
| You can donate via paypal:
95+
|
96+
| You can donate via paypal:
9297
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
9398
<input type="hidden" name="cmd" value="_s-xclick">
9499
<input type="hidden" name="hosted_button_id" value="SFNWWL7FTG34L">
95100
<input type="image" src="https://www.paypalobjects.com/de_DE/DE/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="Jetzt einfach, schnell und sicher online bezahlen – mit PayPal.">
96101
<img alt="" border="0" src="https://www.paypalobjects.com/de_DE/i/scr/pixel.gif" width="1" height="1">
97102
</form>
98-
| Or you can donate via liberapay:
103+
| or with liberapay:
99104
a(href="https://liberapay.com/t3r/",target="_blank")
100-
img(alt="Donate using Liberapay",src="https://liberapay.com/assets/widgets/donate.svg")
105+
img(alt="Donate using Liberapay",src="https://liberapay.com/assets/widgets/donate.svg")
101106
|
102107
nav#content
103108
div#map

0 commit comments

Comments
 (0)