native messaging test file.

This commit is contained in:
Omar Rizwan 2020-10-21 15:13:37 -07:00
parent c78377d464
commit 69c167a134
2 changed files with 40 additions and 4 deletions

View file

@ -289,10 +289,14 @@ async function releasedir(path) {
if (route.releasedir) return route.releasedir(path);
}
function log(...ss) {
console.log(...ss);
}
let port;
/* let ws;*/
async function onMessage(req) {
console.log('req', req);
log('req', req);
let response = { op: req.op, error: unix.EIO };
/* console.time(req.op + ':' + req.path);*/
@ -366,14 +370,16 @@ async function onMessage(req) {
}
/* console.timeEnd(req.op + ':' + req.path);*/
console.log('resp', response);
ws.send(JSON.stringify(response));
log('resp', response);
/* ws.send(JSON.stringify(response));*/
};
function tryConnect() {
port = chrome.runtime.connectNative('com.rsnous.TabFS');
updateToolbarIcon();
/* console.log('hello', port);*/
/* updateToolbarIcon();*/
port.onMessage.addListener(onMessage);
port.onDisconnect.addListener(p => {log(p)});
/* ws = new WebSocket("ws://localhost:8888");
* updateToolbarIcon();

30
fs/test-native.c Normal file
View file

@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
FILE *log = fopen("log.txt", "w");
fprintf(log, "hello\n"); fflush(log);
for (;;) {
char *outMsg = "{\"text\":\"This is a response message\"}";
unsigned int outLen = strlen(outMsg);
char *bOutLen = (char *)&outLen;
write(1, bOutLen, 4); // 1 is stdout
write(1, outMsg, outLen);
fflush(stdout);
fprintf(log, "wrote msg\n"); fflush(log);
char bInLen[4];
read(0, bInLen, 4); // 0 is stdin
unsigned int inLen = *(unsigned int *)bInLen;
char *inMsg = (char *)malloc(inLen);
read(0, inMsg, inLen);
inMsg[inLen] = '\0';
fprintf(log, "msg: [%s]\n", inMsg); fflush(log);
free(inMsg);
}
return 0;
}