Skip to content

Commit

Permalink
Provide number of clients
Browse files Browse the repository at this point in the history
  • Loading branch information
t3r committed Oct 9, 2018
1 parent 36f7365 commit f7d9c8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
24 changes: 21 additions & 3 deletions routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ function ServerObserver() {
}

ServerObserver.prototype.loop = function() {

let self = this;
for( srv in this.observers ) {

GetCachedStatus(srv,5001)
.then( data => {
var toSend = JSON.stringify({
data: data,
nrOfClients: self.getNrOfClients(),
})
this.observers[srv].forEach( ws => {
try {
ws.send( JSON.stringify(data) )
ws.send( toSend )
}
catch( ex ) {
this.unsubscribe( ws );
Expand All @@ -150,6 +154,7 @@ ServerObserver.prototype.loop = function() {
})
.catch( err => {
console.log("Can't get cached status for ", srv, err );
//TODO: check this context
this.observers[srv].forEach( ws => {
ws.close()
})
Expand All @@ -161,14 +166,27 @@ ServerObserver.prototype.loop = function() {

}

ServerObserver.prototype.getNrOfClients = function(server) {
let reply = 0
for( var server in this.observers ) {
reply += this.observers[server].length
}
return reply
}

ServerObserver.prototype.subscribe = function(server,ws) {
//console.log("subscribe",server,ws._socket.remoteAddress);
let self = this;
this.unsubscribe(ws);
(this.observers[server] = (this.observers[server] || [])).push(ws);
try {
GetCachedStatus(server,5001)
.then( data => {
try {
ws.send( JSON.stringify(data) )
ws.send( JSON.stringify({
data: data,
nrOfClients: self.getNrOfClients(),
}))
}
catch( ex ) {
this.unsubscribe( ws );
Expand Down
39 changes: 4 additions & 35 deletions static/mpmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,46 +220,15 @@ $(function() {

function haveData( data ) {
setConnected(true);
data.clients.sort(function(a,b) {
data.data.clients.sort(function(a,b) {
return a.callsign.localeCompare(b.callsign)
})
aircraftLayer.fire('mpdata',{ data: data },aircraftLayer)
setPilotsList( data.clients )
aircraftLayer.fire('mpdata',{ data: data.data },aircraftLayer)
setPilotsList( data.data.clients )
$("#nrOfClients").html(data.nrOfClients);
}

var retryCnt = 3
function loadData() {
$.ajax( "api/stat/" + settings.server, {
success: function(data) {
haveData( data );
if( settings.refresh > 0 ) {
setTimeout( function() {
loadData()
},settings.refresh*1000)
}
},
error: function(a,b,c) {
setConnected(false);
console.log("AJAX error, retry=", retryCnt,a,b,c)
if( retryCnt > 0 ) {
retryCnt--;
if( settings.refresh > 0 ) {
setTimeout( function() {
loadData()
},settings.refresh*1000)
}
} else {
alert("Can't load multiplayer data, please reload page")
}
},
timeout: 30000,
})
}

// loadData()

var ws = null;

function createWebsocket() {
var wsUrl = (window.location.protocol === 'https' ? 'wss' : 'ws') +
'://' + window.location.host + '/api/stream';
Expand Down
21 changes: 13 additions & 8 deletions views/main.pug
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,30 @@ html(lang="en")
span.fas.fa-info-circle
span  About
div#collapseThree.collapse(data-parent="#accordion",role="tabpanel",aria-labelledby="headingThree",aria-expanded="false")
div.card-body
p
div.card-body
p
| This is <a href="http://www.flightgear.org">FlightGear</a> μ (mju) aka mpmap.js, a multiplayer map for based on node.js with
| the <a href="https://github.com/t3r/mpmap.js">source code on github</a>.
p
| If you like this map and use it regularly, please consider donating
| for the hosting and bandwidth costs. Donations also help operating the
| If you like this map and use it regularly (it looks like
|
span.font-weight-bold#nrOfClients 0
|
| users enjoy it at the moment), please consider donating
| for the hosting and bandwidth costs. Donations also help operating the
|
a(href="https://scenery.flightgear.org",target="_blank") FlightGear scenemodels database.
|
| You can donate via paypal:
|
| You can donate via paypal:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="SFNWWL7FTG34L">
<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.">
<img alt="" border="0" src="https://www.paypalobjects.com/de_DE/i/scr/pixel.gif" width="1" height="1">
</form>
| Or you can donate via liberapay:
| or with liberapay:
a(href="https://liberapay.com/t3r/",target="_blank")
img(alt="Donate using Liberapay",src="https://liberapay.com/assets/widgets/donate.svg")
img(alt="Donate using Liberapay",src="https://liberapay.com/assets/widgets/donate.svg")
|
nav#content
div#map
Expand Down

0 comments on commit f7d9c8d

Please sign in to comment.