smart player afk detection added

This commit is contained in:
2023-04-03 12:38:52 +03:00
parent d9e2ae0f53
commit 063e8c25b7
2 changed files with 62 additions and 0 deletions
+11
View File
@@ -372,6 +372,7 @@ streamerServer.on('connection', function (ws, req) {
ws.on('message', (msgRaw) => {
var msg;
try {
msg = JSON.parse(msgRaw);
} catch(err) {
@@ -622,6 +623,13 @@ playerServer.on('connection', function (ws, req) {
p.ws.send(playerCountMsg);
}
}
function sendPlayersLastInputTime() {
let playerCountMsg = JSON.stringify({ type: 'lastOtherPlayerInput'});
for (let p of players.values()) {
p.ws.send(playerCountMsg);
}
}
ws.on('message', (msgRaw) =>{
@@ -659,6 +667,9 @@ playerServer.on('connection', function (ws, req) {
} else if (msg.type == "peerDataChannelsReady") {
msg.playerId = playerId;
sendMessageToController(msg, skipSFU, true);
} else if (msg.type == "playerInput") {
//msg.playerId = playerId;
sendPlayersLastInputTime();
}
else {
console.error(`player ${playerId}: unsupported message type: ${msg.type}`);
@@ -27,6 +27,51 @@ class TwoWayMap {
delete this.reverseMap[value];
}
}
// afk
const get_time_in_seconds = () => {
return new Date().getTime() / 1000;
}
let lastInputTime
let lastInputSendTime = 0
let inputSendTimeout = 3
let cirrusWebsocket = null
let lastOtherPlayerInput = get_time_in_seconds()
function getOveralLastPlayerInput() {
return lastOtherPlayerInput
}
function updateLastInputTime() {
lastInputTime = get_time_in_seconds()
}
updateLastInputTime()
function updateLastOtherPlayerInput() {
//console.log('last player input')
lastOtherPlayerInput = get_time_in_seconds()
}
const check_input = () => {
//console.log('time from last input: ', get_time_in_seconds() - getOveralLastPlayerInput())
if (!cirrusWebsocket)
return
if (lastInputSendTime == lastInputTime) {
return
}
if ((get_time_in_seconds() - lastInputSendTime) > inputSendTimeout) {
lastInputSendTime = lastInputTime
cirrusWebsocket.send(JSON.stringify({type:"playerInput"}))
}
}
const start_check_input = () => {
check_input()
setTimeout(() => start_check_input(), 1000)
}
start_check_input()
/**
* Frontend logic
@@ -1153,6 +1198,7 @@ function createWebRtcOffer() {
function sendInputData(data) {
if (webRtcPlayerObj) {
updateLastInputTime();
resetAfkWarningTimer();
webRtcPlayerObj.send(data);
}
@@ -2744,6 +2790,7 @@ function connect() {
}
ws.onmessage = function (event) {
cirrusWebsocket = ws
// Check if websocket message is binary, if so, stringify it.
if (event.data && event.data instanceof Blob) {
@@ -2771,6 +2818,8 @@ function connect() {
console.warn(msg.warning);
} else if (msg.type === 'peerDataChannels') {
onWebRtcSFUPeerDatachannels(msg);
} else if (msg.type === 'lastOtherPlayerInput') {
updateLastOtherPlayerInput();
} else {
console.error("Invalid SS message type", msg.type);
}
@@ -2778,9 +2827,11 @@ function connect() {
ws.onerror = function (event) {
console.log(`WS error: ${JSON.stringify(event)}`);
cirrusWebsocket = null
};
ws.onclose = function (event) {
cirrusWebsocket = null
closeStream();