Skip to content

Commit 9dc3259

Browse files
committed
[webgui] introduce simple RWebWindow tutorial
It shows simple communication between c++ server and JS client
1 parent 00d0d60 commit 9dc3259

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5+
<meta charset="utf-8">
6+
<title>RWebWindow test</title>
7+
8+
<script src="/jsrootsys/scripts/JSRootCore.js" type="text/javascript"></script>
9+
</head>
10+
11+
<body>
12+
<button onclick="SendMsg('get_text')">Text</button>
13+
<button onclick="SendMsg('get_binary')">Binary</button>
14+
<button onclick="SendMsg('halt')">Halt</button>
15+
<div id="main"></div>
16+
</body>
17+
18+
<script>
19+
20+
var conn_handle = null;
21+
22+
function SendMsg(txt) {
23+
if (conn_handle)
24+
conn_handle.Send(txt);
25+
}
26+
27+
function AddOutput(msg) {
28+
document.getElementById("main").innerHTML += msg + "<br>";
29+
}
30+
31+
JSROOT.ConnectWebWindow({
32+
// prereq: "2d", /* one could specify different JSROOT components like 2d or geom
33+
// prereq_logdiv: "main", /* div id where loading of JSROOT scripts will be logged */
34+
// callback: function(handle) { AddOutput("init done"); }, /* if necessary, initial callback can be cacthed */
35+
receiver: {
36+
// method called when connection to server is established
37+
OnWebsocketOpened: function(handle) {
38+
handle.Send("Init msg from client");
39+
conn_handle = handle;
40+
AddOutput("Connected");
41+
},
42+
43+
// method with new message from server
44+
OnWebsocketMsg: function(handle, msg, offset) {
45+
if (typeof msg != "string") {
46+
var arr = new Float32Array(msg, offset);
47+
AddOutput("bin: " + arr.toString());
48+
} else {
49+
AddOutput("txt: " + msg);
50+
}
51+
},
52+
53+
// method called when connection is gone
54+
OnWebsocketClosed: function(handle) {
55+
conn_handle = null;
56+
// when connection closed, close panel as well
57+
if (window) window.close();
58+
}
59+
}
60+
});
61+
62+
</script>
63+
64+
</html>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// \file
2+
/// \ingroup tutorial_webgui
3+
/// This program demonstrates minimal server/client code for working with RWebWindow class
4+
/// File server.cxx shows how RWebWindow can be created and used
5+
/// In client.html simple client code is provided.
6+
///
7+
/// \macro_code
8+
///
9+
/// \author Sergey Linev
10+
11+
12+
#include <ROOT/RWebWindowsManager.hxx>
13+
14+
std::shared_ptr<ROOT::Experimental::RWebWindow> window;
15+
16+
int counter{0};
17+
18+
void ProcessData(unsigned connid, const std::string &arg)
19+
{
20+
if (arg == "CONN_READY") {
21+
printf("connection established %u\n", connid);
22+
return;
23+
}
24+
25+
if (arg == "CONN_CLOSED") {
26+
printf("connection closed\n");
27+
return;
28+
}
29+
30+
printf("Get msg %s \n", arg.c_str());
31+
32+
counter++;
33+
34+
if (arg == "get_text") {
35+
// send arbitrary text message
36+
window->Send(connid, Form("Message%d", counter));
37+
} else if (arg == "get_binary") {
38+
// send float array as binary
39+
float arr[10];
40+
for (int n = 0; n < 10; ++n)
41+
arr[n] = counter;
42+
window->SendBinary(connid, arr, sizeof(arr));
43+
} else if (arg == "halt") {
44+
// terminate ROOT
45+
ROOT::Experimental::RWebWindowsManager::Instance()->Terminate();
46+
}
47+
}
48+
49+
void server()
50+
{
51+
// create window, manager can handle many windows a time
52+
window = ROOT::Experimental::RWebWindowsManager::Instance()->CreateWindow();
53+
54+
// configure default html page
55+
// either HTML code can be specified or just name of file after 'file:' prefix
56+
window->SetDefaultPage("file:client.html");
57+
58+
// this is call-back, invoked when message received from client
59+
window->SetDataCallBack(ProcessData);
60+
61+
window->SetGeometry(300, 500); // configure predefined geometry
62+
63+
window->Show();
64+
}

0 commit comments

Comments
 (0)