cleanup and tweaks

This commit is contained in:
Eric House 2021-03-12 07:05:21 -08:00
parent f48c7ce0f3
commit 1aa3ddcde0
4 changed files with 27 additions and 16 deletions

View file

@ -157,7 +157,6 @@ dvc_getMQTTDevID( XW_DUtilCtxt* dutil, XWEnv xwe, MQTTDevID* devID )
void
dvc_resetMQTTDevID( XW_DUtilCtxt* dutil, XWEnv xwe )
{
LOG_FUNC();
#ifdef BOGUS_ALL_SAME_DEVID
XP_LOGFF( "doing nothing" );
XP_USE( dutil );
@ -193,7 +192,6 @@ void
dvc_makeMQTTInvite( XW_DUtilCtxt* dutil, XWEnv xwe, XWStreamCtxt* stream,
const NetLaunchInfo* nli )
{
LOG_FUNC();
addHeaderGameIDAndCmd( dutil, xwe, CMD_INVITE, nli->gameID, stream );
nli_saveToStream( nli, stream );
}
@ -202,7 +200,6 @@ void
dvc_makeMQTTMessage( XW_DUtilCtxt* dutil, XWEnv xwe, XWStreamCtxt* stream,
XP_U32 gameID, const XP_U8* buf, XP_U16 len )
{
LOG_FUNC();
addHeaderGameIDAndCmd( dutil, xwe, CMD_MSG, gameID, stream );
stream_putBytes( stream, buf, len );
}

View file

@ -326,14 +326,14 @@ call_alert( const char* msg )
static bool
sendStreamToDev( XWStreamCtxt* stream, const MQTTDevID* devID )
{
XP_S16 nSent = -1;
XP_UCHAR topic[64];
formatMQTTTopic( devID, topic, sizeof(topic) );
XP_U16 streamLen = stream_getSize( stream );
bool success = call_mqttSend( topic, stream_getPtr( stream ), streamLen );
stream_destroy( stream, NULL );
LOG_RETURNF("%d", nSent);
XP_LOGFF("(to: %s) => %s", topic, boolToStr(success) );
return success;
}

View file

@ -114,11 +114,10 @@
<input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked,
document.getElementById('resize').checked)">
</div>
<hr/>
-->
<textarea class="emscripten" id="output" rows="8"></textarea>
<hr>
-->
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');

View file

@ -1,6 +1,9 @@
var state = {client: null,
closure: null,
connected: false,
disconnTimeMS: 0,
connTimeMS: 0,
connChangeStamp: 0,
};
function ccallString(proc, closure, str) {
@ -144,7 +147,23 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
document.getElementById("mqtt_span").textContent=devid;
function tellConnected(isConn) {
function onConnChange(isConn) {
state.connected = isConn;
const now = Date.now();
if ( 0 != state.connChangeStamp ) {
const incr = now - state.connChangeStamp;
if ( isConn ) {
state.disconnTimeMS += incr;
} else {
state.connTimeMS += incr;
}
}
state.connChangeStamp = now;
let stateStr = isConn ? 'Connected' : 'Disconnected';
document.getElementById("mqtt_status").textContent = stateStr;
Module.ccall('MQTTConnectedChanged', null, ['number', 'boolean'],
[state.closure, isConn]);
}
@ -153,9 +172,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
// set callback handlers
state.client.onConnectionLost = function onConnectionLost(responseObject) {
state.connected = false;
document.getElementById("mqtt_status").textContent="Disconnected";
tellConnected(false);
onConnChange(false);
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
@ -168,9 +185,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
};
function onConnect() {
state.connected = true
document.getElementById("mqtt_status").textContent="Connected";
tellConnected(true);
onConnChange(true);
var subscribeOptions = {
qos: 2, // QoS
@ -188,7 +203,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
useSSL: true,
reconnect: true,
onSuccess: onConnect,
onFailure: function() { alert('onFailure'); },
onFailure: function() { console.error('mqtt.client.connect.onFailure'); },
});
}
@ -201,7 +216,7 @@ function mqttSend( topic, ptr ) {
message.qos = 2;
state.client.send(message);
} else {
console.log('mqttSend: not connected');
console.error('mqttSend: not connected');
}
return canSend;
}