Skip to content

Commit 0379cb1

Browse files
author
MYK
committed
messaging from cpp -> web ui
1 parent 533be0e commit 0379cb1

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ The embedded web server is the header-only cpp-httplib by Yuji Hirose from here:
2525
# TODO
2626

2727
* [DONE] web interface -> cpp comms
28-
* cpp -> web interface comms
28+
* [DONE except annoying assert] cpp -> web interface comms
2929
* test on mac and windows

src/HTTPServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ void HttpServerThread::initAPI()
4545

4646
// 'live' responders for button presses - call to the pluginprocessor
4747
svr.Get("/button1", [this](const httplib::Request& req, httplib::Response& res) {
48-
this->pluginProc.api_sendMessage("Button 1 clicked");
48+
this->pluginProc.messageReceivedFromWebAPI("Button 1 clicked");
4949
});
5050
svr.Get("/button2", [this](const httplib::Request& req, httplib::Response& res) {
51-
this->pluginProc.api_sendMessage("Button 2 clicked");
51+
this->pluginProc.messageReceivedFromWebAPI("Button 2 clicked");
5252
});
5353

5454

src/PluginEditor.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
PluginEditor::PluginEditor (PluginProcessor& p)
66
: AudioProcessorEditor (&p), processorRef (p),
77
webView{
8+
// no idea if this stuff is needed... but it looks useful
89
juce::WebBrowserComponent::Options{}
910
.withBackend(juce::WebBrowserComponent::Options::Backend::webview2)
1011
.withWinWebView2Options(juce::WebBrowserComponent::Options::WinWebView2{}
@@ -42,3 +43,19 @@ void PluginEditor::resized()
4243
{
4344
webView.setBounds(getLocalBounds()); // Make web view fill entire UI
4445
}
46+
47+
48+
void PluginEditor::updateUIFromProcessor(const juce::String& msg)
49+
{
50+
// create an object and convert it to a json string
51+
juce::DynamicObject::Ptr obj = new juce::DynamicObject();
52+
obj->setProperty("msg", msg);
53+
juce::var data(obj);
54+
juce::String json = juce::JSON::toString(data);
55+
56+
webView.evaluateJavascript("handleCppMessage(" + json + ");", nullptr);
57+
58+
// std::string msg_as_json = "{\"msg\":\""+ msg + "\"}";
59+
// juce::JSON::toString({"msg":msg});
60+
// webView.evaluateJavascript("backendCall('"+msg_as_json+"')");
61+
}

src/PluginEditor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class PluginEditor final : public juce::AudioProcessorEditor
1515
void paint (juce::Graphics&) override;
1616
void resized() override;
1717

18+
void updateUIFromProcessor(const juce::String& msg);
19+
1820
private:
1921
// This reference is provided as a quick way for your editor to
2022
// access the processor object that created it.

src/PluginProcessor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
193193

194194

195195

196-
void PluginProcessor::api_sendMessage(std::string msg)
196+
void PluginProcessor::messageReceivedFromWebAPI(std::string msg)
197197
{
198198
DBG("PluginProcess received a message " << msg);
199+
if (auto* editor = dynamic_cast<PluginEditor*>(getActiveEditor()))
200+
{
201+
editor->updateUIFromProcessor("Got your message " + msg);
202+
}
199203
}
200204

src/PluginProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PluginProcessor final : public juce::AudioProcessor
5050
// things for the api server to call
5151
// really you should make an 'interface' for this
5252
// but for simplicity in this demo
53-
void api_sendMessage(std::string msg);
53+
void messageReceivedFromWebAPI(std::string msg);
5454

5555
private:
5656
HttpServerThread apiServer;

src/ui/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
<!DOCTYPE html>
12
<html>
23

34
<body>
45

56
<h1>I am served</h1>
67
<button onclick="callAPI('button1')">Hit me</button>
78
<button onclick="callAPI('button2')">and me</button>
9+
<div id="messages"></div>
10+
811
<script>
912

13+
// the cpp back end will send us messages
14+
function handleCppMessage(data){
15+
// data = JSON.parse(json_string);
16+
console.log(data["msg"])
17+
msg_div = document.getElementById("messages");
18+
msg_div.innerText = data.msg;
19+
20+
// Optional: send dummy result back so C++ doesn't assert
21+
if (window.__JUCE__?.emitResult)
22+
window.__JUCE__.emitResult("ok");
23+
}
1024

25+
// the buttons call this function to send messages
26+
// to the API server hosted in the PluginProcessor - direct comms to the pluginprocessor :)
1127
function callAPI(endpoint){
1228
fetch("/" + endpoint)
1329
.then(response => {
30+
console.log("Web UI: response from API " + endpoint)
1431
console.log(response.toString())
1532
})
1633
.catch(error => {

0 commit comments

Comments
 (0)