Remove duplicate worker addresses during ping check (client, server) (#234)

This commit is contained in:
sergystepanov 2020-10-03 18:53:46 +03:00 committed by GitHub
parent 564b96df8d
commit 453087ed28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions

View file

@ -342,14 +342,18 @@ func (o *Server) findBestServerFromBrowser(workerClients map[string]*WorkerClien
// getLatencyMapFromBrowser get all latencies from worker to user
func (o *Server) getLatencyMapFromBrowser(workerClients map[string]*WorkerClient, client *BrowserClient) map[*WorkerClient]int64 {
workersList := []*WorkerClient{}
addressList := []string{}
var workersList []*WorkerClient
var addressList []string
uniqueAddresses := map[string]bool{}
latencyMap := map[*WorkerClient]int64{}
// addressList is the list of worker addresses
for _, workerClient := range workerClients {
if _, ok := uniqueAddresses[workerClient.PingServer]; !ok {
addressList = append(addressList, workerClient.PingServer)
}
uniqueAddresses[workerClient.PingServer] = true
workersList = append(workersList, workerClient)
addressList = append(addressList, workerClient.PingServer)
}
// send this address to user and get back latency

2
web/game.html vendored
View file

@ -128,7 +128,7 @@
<script src="/static/js/network/ajax.js?v=3"></script>
<script src="/static/js/network/socket.js?v=4"></script>
<script src="/static/js/network/rtcp.js?v=3"></script>
<script src="/static/js/controller.js?v=5"></script>
<script src="/static/js/controller.js?v=6"></script>
<script src="/static/js/input/keyboard.js?v=5"></script>
<script src="/static/js/input/touch.js?v=3"></script>
<script src="/static/js/input/joystick.js?v=3"></script>

31
web/js/controller.js vendored
View file

@ -77,21 +77,22 @@
}
};
const onLatencyCheckRequest = (data) => {
popup('Ping check...');
const timeoutMs = 2000;
const onLatencyCheck = (data) => {
popup('Connecting to fastest server...');
const timeoutMs = 1111;
// deduplicate
const addresses = [...new Set(data.addresses || [])];
Promise.all((data.addresses || [])
.map(ip => {
const requestTime = Date.now();
return ajax.fetch(`${ip}?_=${requestTime}`, {method: "GET", redirect: "follow"}, timeoutMs)
.then(() => ({[ip]: Date.now() - requestTime}), () => ({[ip]: timeoutMs}));
}))
.then(results => {
const latencies = Object.assign({}, ...results);
log.info('[ping] <->', latencies);
socket.latency(latencies, data.packetId);
});
Promise.all(addresses.map(address => {
const start = Date.now();
return ajax.fetch(`${address}?_=${start}`, {method: "GET", redirect: "follow"}, timeoutMs)
.then(() => ({[address]: Date.now() - start}))
.catch(() => ({[address]: 9999}));
})).then(servers => {
const latencies = Object.assign({}, ...servers);
log.info('[ping] <->', latencies);
socket.latency(latencies, data.packetId);
});
};
const helpScreen = {
@ -436,7 +437,7 @@
event.sub(MEDIA_STREAM_READY, () => rtcp.start());
event.sub(CONNECTION_READY, onConnectionReady);
event.sub(CONNECTION_CLOSED, () => input.poll().disable());
event.sub(LATENCY_CHECK_REQUESTED, onLatencyCheckRequest);
event.sub(LATENCY_CHECK_REQUESTED, onLatencyCheck);
event.sub(GAMEPAD_CONNECTED, () => popup('Gamepad connected'));
event.sub(GAMEPAD_DISCONNECTED, () => popup('Gamepad disconnected'));
// touch stuff