Correct way of sharing data between processes
Posted: Tue Oct 18, 2022 10:06 am
Hello,
I am just asking myself if I am using the correct (best) way to share data between the browser instance and process logic.
Example from a working application I have in prod. I am using mORMot under the hood, with WebSockets to receive data from a server:
Then on received message, I set `window.json_data` with `ExecuteJavascriptCode`. I have a doubt as when I read the example JSSharedMemoryProcMessage, messages are handled in `GlobalCEFApp_OnProcessMessageReceived`, but I cannot manage to update `window.json_data` from `GlobalCEFApp_OnProcessMessageReceived`.
Everything works, but is there any advice on what I am doing wrong ?
I am just asking myself if I am using the correct (best) way to share data between the browser instance and process logic.
Example from a working application I have in prod. I am using mORMot under the hood, with WebSockets to receive data from a server:
Code: Select all
procedure TUptimeStatusCallback.NotifyUptimeStatus(const msg: RawUtf8);
var copyDataStruct : TCopyDataStruct;
begin
copyDataStruct.dwData := MSG_NEW_CAPTORS;
copyDataStruct.cbData := Length(msg) * SizeOf(AnsiChar);
copyDataStruct.lpData := PAnsiChar(msg);
PostMessage(frmMain.Handle, MSG_NEW_CAPTORS, 0, LPARAM(@copyDataStruct)); // send data to main form, should I send it directly to the browser ?
end;
Code: Select all
procedure TfrmMain.GetNewCaptors(var aMessage: TMessage);
var p : PCopyDataStruct;
S: RawUtf8;
begin
// data is a large JSON string
p := PCopyDataStruct(aMessage.lParam);
if (p <> nil) and (p^.dwData = MSG_NEW_CAPTORS) then
begin
var S2: TRawByteStringBuffer;
S2.Append(PAnsiChar(p^.lpData), p^.cbData);
S2.AsText(S);
var code := FormatUtf8('window.json_data=''%'';', [S]);
Chromium1.ExecuteJavaScript(code, '/', 0);
end;
end;