snapshot: play via MQTT works for a while

This commit is contained in:
Eric House 2023-02-22 19:52:45 -08:00
parent 4bc24b41f6
commit 5382941f63
4 changed files with 44 additions and 13 deletions

View file

@ -1910,10 +1910,10 @@ freeElem( MPFORMAL MsgQueueElem* elem )
{
XP_FREEP( mpool, &elem->msg );
#ifdef COMMS_CHECKSUM
XP_LOGFF( "freeing msg with len %d, sum %s", elem->len, elem->checksum );
/* XP_LOGFF( "freeing msg with len %d, sum %s", elem->len, elem->checksum ); */
XP_FREEP( mpool, &elem->checksum );
#else
XP_LOGFF( "freeing msg with len %d", elem->len );
/* XP_LOGFF( "freeing msg with len %d", elem->len ); */
#endif
XP_FREE( mpool, elem );
}

View file

@ -86,7 +86,7 @@ main.html: ${INPUTS} Makefile shell_minimal.html
--preload-file assets_dir --shell-file shell_minimal.html \
-s USE_SDL_TTF=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' -lidbfs.js \
-s "EXPORTED_RUNTIME_METHODS=['ccall']" \
-s EXPORTED_FUNCTIONS='["_main", "_mainPostSync", "_cbckBinary", "_cbckVoid", "_cbckString", "_MQTTConnectedChanged", "_onNewGame", "_gotDictBinary", "_onResize"]' \
-s EXPORTED_FUNCTIONS='["_main", "_mainPostSync", "_cbckBinary", "_cbckMsg", "_cbckVoid", "_cbckString", "_MQTTConnectedChanged", "_onNewGame", "_gotDictBinary", "_onResize"]' \
-s WASM=1 \
${INPUTS} -o $@

View file

@ -201,12 +201,17 @@ EM_JS(void, call_get_string, (const char* msg, const char* dflt,
EM_JS(void, call_setup, (void* closure, bool dbg, const char* devid,
const char* gitrev, int now,
StringProc conflictProc,
StringProc focussedProc,
MsgProc msgProc), {
StringProc conflictProc, StringProc focussedProc,
MsgProc msgProc,
XP_U16 nTopics, XP_UCHAR** topics), {
let jsgr = UTF8ToString(gitrev);
let subTopics = [];
for (let ii = 0; ii < nTopics; ++ii ) {
const topicMem = HEAP32[(topics + (ii * 4)) >> 2];
subTopics[ii] = UTF8ToString(topicMem);
}
jssetup(closure, dbg, UTF8ToString(devid), jsgr, now,
conflictProc, focussedProc, msgProc);
conflictProc, focussedProc, msgProc, subTopics);
});
EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), {
@ -1122,8 +1127,19 @@ initDeviceGlobals( Globals* globals )
false
#endif
;
XP_UCHAR storage[256];
XP_UCHAR* topics[4];
XP_U16 nTopics = VSIZE(topics);
dvc_getMQTTSubTopics( globals->dutil, NULL_XWE,
storage, VSIZE(storage),
&nTopics, topics );
for ( int ii = 0; ii < nTopics; ++ii ) {
XP_LOGFF( "got topic %d: %s", ii, topics[ii] );
}
call_setup( globals, dbg, buf, GITREV, now, onConflict,
onFocussed, onMqttMsg );
onFocussed, onMqttMsg, nTopics, topics );
}
static void
@ -2081,7 +2097,14 @@ MQTTConnectedChanged( void* closure, bool connected )
void
cbckBinary( BinProc proc, void* closure, int len, const uint8_t* msg )
{
(*proc)(closure, msg, len );
(*proc)( closure, msg, len );
}
void
cbckMsg( MsgProc proc, void* closure, const char* topic,
int len, const uint8_t* msg )
{
(*proc)( closure, topic, msg, len );
}
static void

View file

@ -122,7 +122,8 @@ function getDict(langs, proc, closure) {
}
// Called from main() asap after things are initialized etc.
function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc) {
function jssetup(closure, dbg, devid, gitrev, now, noTabProc,
focusProc, msgProc, subTopics) {
// Set a unique tag so we know if somebody comes along later
let tabID = Math.random();
let item = 'tabID/' + dbg;
@ -146,6 +147,7 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
state.closure = closure;
state.msgProc = msgProc;
state.subTopics = subTopics;
registerOnce(devid, gitrev, now, dbg);
@ -185,10 +187,11 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
}
};
state.client.onMessageArrived = function onMessageArrived(message) {
let topic = message.topic;
let payload = message.payloadBytes;
let length = payload.length;
Module.ccall('cbckBinary', null, ['number', 'number', 'number', 'array'],
[state.msgProc, state.closure, length, payload]);
Module.ccall('cbckMsg', null, ['number', 'number', 'string', 'number', 'array'],
[state.msgProc, state.closure, topic, length, payload]);
};
function onConnect() {
@ -201,7 +204,11 @@ function jssetup(closure, dbg, devid, gitrev, now, noTabProc, focusProc, msgProc
onFailure: function() { alert('subscribe failed'); },
timeout: 10,
};
state.client.subscribe('xw4/device/' + devid, subscribeOptions);
for ( topic of state.subTopics ) {
// console.log('calling subscribe('+topic+')');
state.client.subscribe(topic, subscribeOptions);
}
}
state.client.connect({mqttVersion: 3,
@ -247,6 +254,7 @@ function mqttSend( topic, ptr ) {
message = new Paho.MQTT.Message(ptr);
message.destinationName = topic;
message.qos = 2;
message.retained = true;
state.client.send(message);
} else {
console.error('mqttSend: not connected');