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

View file

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

View file

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

View file

@ -1,6 +1,9 @@
var state = {client: null, var state = {client: null,
closure: null, closure: null,
connected: false, connected: false,
disconnTimeMS: 0,
connTimeMS: 0,
connChangeStamp: 0,
}; };
function ccallString(proc, closure, str) { 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; 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'], Module.ccall('MQTTConnectedChanged', null, ['number', 'boolean'],
[state.closure, isConn]); [state.closure, isConn]);
} }
@ -153,9 +172,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
// set callback handlers // set callback handlers
state.client.onConnectionLost = function onConnectionLost(responseObject) { state.client.onConnectionLost = function onConnectionLost(responseObject) {
state.connected = false; onConnChange(false);
document.getElementById("mqtt_status").textContent="Disconnected";
tellConnected(false);
if (responseObject.errorCode !== 0) { if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage); console.log("onConnectionLost:"+responseObject.errorMessage);
} }
@ -168,9 +185,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
}; };
function onConnect() { function onConnect() {
state.connected = true onConnChange(true);
document.getElementById("mqtt_status").textContent="Connected";
tellConnected(true);
var subscribeOptions = { var subscribeOptions = {
qos: 2, // QoS qos: 2, // QoS
@ -188,7 +203,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
useSSL: true, useSSL: true,
reconnect: true, reconnect: true,
onSuccess: onConnect, 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; message.qos = 2;
state.client.send(message); state.client.send(message);
} else { } else {
console.log('mqttSend: not connected'); console.error('mqttSend: not connected');
} }
return canSend; return canSend;
} }