你有一兩個網頁嗎?不一定是多神奇的東西,幾個 HTML 標籤做出的簡潔示範頁就夠了。你手上也有一個需要遠端控制的複雜 C++ Windows 桌面程式嗎?那麼不必去學一整套新技術,我們直接為應用加上網頁介面。
Webem 是一個可以嵌入 C++ 應用程式的 Web 伺服器,能輕鬆實作出任何地方都能存取的瀏覽器 GUI。它基於一個簡化版的 boost::asio Web 伺服器,讓 HTML 能觸發 C++ 方法。使用 Webem 不需要閱讀伺服器程式碼,但你的專案必須連結 Boost 函式庫;如果你從沒用過 Boost,Webem 可能不適合你。
原始碼下載:https://linux.linuxidc.com/(帳號與密碼皆為 www.linuxidc.com),目錄 /2014年資料/10月/26日/一個簡單的 C++ 嵌入 Web 服務器。
現有的嵌入式 C++ Web 伺服器往往不好上手,也不太對 Windows 友善,並非拿來替實驗性應用加入手機監控能力的好選擇。我試過 https://www.webtoolkit.eu/wt,卻卡在安裝與學習階段。
後來我開始使用 John Bartas 的 Webio,理念很好、運作也順,但仍嫌過於複雜,伺服端程式碼不易理解。我真正想要的是:容易使用、以知名 Web 伺服器為基礎、只做了輕微修改。
Webio 的複雜度有相當部分來自它用一個 HTML 編譯器,把嵌在應用程式碼裡的檔案系統外觀隱藏起來。我更偏好把 HTML 頁面放在外部,這樣不必重新編譯就能調整 GUI。最終我決定自己打造一個符合需求的版本。
你可以像建網站一樣建立應用程式的 GUI——從 index.html 開始寫頁面。接著讓 HTML 能呼叫你的 C++ 方法,需要三件事:
第一步,建立網頁:
The Webem Embedded Web server says: <!--#webem hello -->
角括號中的文字告訴 Webem 要在何處插入內容,hello 則是要被呼叫的應用方法。
第二步,建立回傳 hello 的類別:
/// An application class which says hello
class cHello
{
public:
char * DisplayHTML()
{
return "Hello World";
}
};
第三步,初始化 Webem,設定監聽位址、連接埠與文件根目錄:
// Initialize web server.
http::server::cWebem theServer(
"0.0.0.0", // address
"1570", // port
".\\"); // document root
第四步,註冊應用方法:
cHello hello;
// 每當伺服器看到 <!--#webem hello -->
// 就呼叫 cHello::DisplayHTML() 並插入回傳的 HTML
theServer.RegisterIncludeCode( "hello",
boost::bind(
&cHello::DisplayHTML, // member function
&hello ) ); // instance of class
第五步,啟動服務:
// run the server theServer.Run();
接著做一個能依姓名打招呼的版本。第一步,建立站點:
What is your name, please? <form action=name.webem> <input name=yourname /><input value="Enter" type=submit /> </form> The Webem Embedded Web server says: <!--#webem hello -->
表單屬性 action=name.webem 確保 Webem 會呼叫以 name 註冊的方法來處理輸入。
第二步,建立應用類別:
/// An application class which says hello to the identified user
class cHelloForm
{
string UserName;
http::server::cWebem& myWebem;
public:
cHelloForm( http::server::cWebem& webem ) :
myWebem( webem )
{
myWebem.RegisterIncludeCode( "hello",
boost::bind(
&cHelloForm::DisplayHTML, // member function
this ) ); // instance of class
myWebem.RegisterActionCode( "name",
boost::bind(
&cHelloForm::Action, // member function
this ) ); // instance of class
}
char * DisplayHTML()
{
static char buf[1000];
if( UserName.length() )
sprintf_s( buf, 999, "Hello, %s", UserName.c_str() );
else
buf[0] = '\0';
return buf;
}
char * Action()
{
UserName = myWebem.FindValue("yourname");
return "/index.html";
}
};
這個類別保存對 Webem 伺服器的引用,因而能在建構時完成自身方法的註冊,並用 FindValue() 取出表單欄位的值。它需要註冊兩個方法:一个在表單送出時保存使用者名稱,一个在頁面組合時顯示名稱。action 方法必須回傳送出後要呈現的網頁。
要注意:所有 action 方法都比 include 方法更早被呼叫,所以網頁永遠呈現的是更新後的資料。
第三步,建立伺服器並啟動:
// Initialize web server http::server::cWebem theServer( "0.0.0.0", // address "1570", // port ".\\"); // document root // Initialize application code cHelloForm hello( theServer ); // run the server theServer.Run();
若你希望伺服器跑在另一條執行緒,讓主程式能繼續記錄實驗裝置的資料,可以改寫成:
boost::thread* pThread = new boost::thread(
boost::bind(
&http::server::server::run, // member function
&theServer ) ); // instance of class
Webem 控制項是把「監視、顯示、操作應用資料」的細節標準化包裝起來的類別,讓開發者不必煩惱 HTML 文字的所有細節。範例程式用一個控制項列出 SQLITE 資料表的全部內容,並提供新增與刪除記錄的能力。
續讀第二頁:https://www.linuxidc.com/Linux/2014-10/108473p2.htm