File tree Expand file tree Collapse file tree 7 files changed +45
-5
lines changed
Expand file tree Collapse file tree 7 files changed +45
-5
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 55PluginEditor::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+ }
Original file line number Diff line number Diff 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+
1820private:
1921 // This reference is provided as a quick way for your editor to
2022 // access the processor object that created it.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
5555private:
5656 HttpServerThread apiServer;
Original file line number Diff line number Diff line change 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 :)
1127function 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 => {
You can’t perform that action at this time.
0 commit comments