get mqtt working again

This commit is contained in:
Eric House 2021-02-09 17:53:30 -08:00
parent a1d6f25fc6
commit 7efc4b29bf
4 changed files with 77 additions and 21 deletions

View file

@ -59,8 +59,8 @@ EM_JS(bool, call_confirm, (const char* str), {
EM_JS(void, call_alert, (const char* str), {
alert(UTF8ToString(str));
});
EM_JS(void, call_haveDevID, (const char* devid), {
onHaveDevID(UTF8ToString(devid));
EM_JS(void, call_haveDevID, (void* closure, const char* devid), {
onHaveDevID(closure, UTF8ToString(devid));
});
EM_JS(void, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), {
@ -135,7 +135,7 @@ initDeviceGlobals( Globals* globals )
XP_UCHAR buf[32];
XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID );
XP_LOGFF( "got mqtt devID: %s", buf );
call_haveDevID( buf );
call_haveDevID( globals, buf );
}
static void
@ -157,9 +157,9 @@ startGame( Globals* globals )
}
(void)server_do( globals->game.server, NULL ); /* assign tiles, etc. */
/* if ( !!globals->game.comms ) { */
/* comms_resendAll( globals->game.comms, NULL, COMMS_CONN_NONE, XP_TRUE ); */
/* } */
if ( !!globals->game.comms ) {
comms_resendAll( globals->game.comms, NULL, COMMS_CONN_MQTT, XP_TRUE );
}
updateScreen( globals );
LOG_RETURN_VOID();
@ -184,7 +184,7 @@ makeAndDraw( Globals* globals, bool forceNew, bool p0robot, bool p1robot )
globals->gi.players[0].isLocal = XP_TRUE;
globals->gi.players[0].robotIQ = p0robot ? 99 : 0;
globals->gi.players[1].name = copyString( globals->mpool, "Player 1" );
globals->gi.players[1].name = copyString( globals->mpool, "Player 2" );
globals->gi.players[1].isLocal = XP_TRUE;
globals->gi.players[1].robotIQ = p1robot ? 99 : 0;
@ -502,11 +502,11 @@ newgame(bool p0, bool p1)
}
void
gotMQTTMsg( int len, const uint8_t* msg )
gotMQTTMsg( void* closure, int len, const uint8_t* msg )
{
XP_LOGFF( "got msg of len %d", len );
dvc_parseMQTTPacket( sGlobals->dutil, NULL, msg, len );
XP_LOGFF( "got msg of len %d (%p vs %p)", len, closure, sGlobals );
Globals* globals = (Globals*)closure;
dvc_parseMQTTPacket( globals->dutil, NULL, msg, len );
}
int

View file

@ -7,6 +7,7 @@
<style>
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.emscripten { font-family: monospace; width: 80%; }
body.centered { text-align: center; }
div.emscripten { text-align: center; }
div.emscripten_border { border: 1px solid black; }
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
@ -46,7 +47,7 @@
</style>
</head>
<body>
<body class="centered">
<hr/>
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
<div class="emscripten" id="status">Downloading...</div>
@ -176,6 +177,10 @@
};
</script>
<script>
var state = {client: null,
closure: null,
};
function callNewGame() {
var args = [
document.getElementById("player0Checked").checked,
@ -183,10 +188,65 @@
]
Module.ccall('newgame', null, ['boolean', 'boolean'], args);
}
function onHaveDevID(devid) {
function onHaveDevID(closure, devid) {
console.log('got ' + devid);
state.closure = closure;
document.getElementById("mqtt_span").textContent=devid;
}
state.client = new Paho.MQTT.Client("eehouse.org", 8883, '/wss', devid);
// set callback handlers
state.client.onConnectionLost = function onConnectionLost(responseObject) {
document.getElementById("mqtt_status").textContent="Disconnected";
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
};
state.client.onMessageArrived = function onMessageArrived(message) {
var payload = message.payloadBytes;
var length = payload.length;
Module.ccall('gotMQTTMsg', null, ['number', 'number', 'array'],
[state.closure, length, payload]);
};
function onConnect() {
document.getElementById("mqtt_status").textContent="Connected";
var subscribeOptions = {
qos: 2, // QoS
// invocationContext: {foo: true}, // Passed to success / failure callback
// onSuccess: function() { alert('subscribe succeeded'); },
onFailure: function() { alert('subscribe failed'); },
timeout: 10,
};
state.client.subscribe('xw4/device/' + devid, subscribeOptions);
}
state.client.connect({mqttVersion: 3,
userName: "xwuser",
password: "xw4r0cks",
useSSL: true,
reconnect: true,
onSuccess: onConnect,
onFailure: function() { alert('onFailure'); },
});
}
function mqttSend (topic, ptr) {
if ( null != state.client ) {
message = new Paho.MQTT.Message(ptr);
message.destinationName = topic;
message.qos = 2;
state.client.send(message);
} else {
alert('null client');
}
}
let script = document.createElement('script');
script.src = "paho-mqtt.js";
document.body.appendChild(script);
</script>
{{{ SCRIPT }}}
</body>

View file

@ -249,9 +249,9 @@ dict_splitFaces( DictionaryCtxt* dict, XWEnv xwe, const XP_U8* utf8,
XP_ASSERT( !dict->facePtrs );
dict->facePtrs = ptrs;
for ( int ii = 0; ii < nFaces; ++ii ) {
XP_LOGFF( "face %d: %s", ii, dict->facePtrs[ii] );
}
/* for ( int ii = 0; ii < nFaces; ++ii ) { */
/* XP_LOGFF( "face %d: %s", ii, dict->facePtrs[ii] ); */
/* } */
} /* dict_splitFaces */
void

View file

@ -359,11 +359,7 @@ drawCrosshairs( WasmDrawCtx* wdctx, const XP_Rect* rect, CellFlags flags )
const SDL_Color* color = &sOtherColors[COLOR_FOCUS];
if ( 0 != (flags & CELL_CROSSHOR) ) {
XP_Rect hairRect = *rect;
XP_LOGFF( "hairRect before: l: %d, t: %d, w: %d, h: %d",
hairRect.left, hairRect.top, hairRect.width, hairRect.height );
insetRect( &hairRect, 0, hairRect.height / 3 );
XP_LOGFF( "hairRect after: l: %d, t: %d, w: %d, h: %d",
hairRect.left, hairRect.top, hairRect.width, hairRect.height );
fillRect( wdctx, &hairRect, color );
}
if ( 0 != (flags & CELL_CROSSVERT) ) {