加入 iPush ActiveX API component 與 VBScript
監控程式要從
iPush Server 接收即時訊息,必須要有 iPush API 元件,以及相關的程式碼。在 IE 瀏覽器上,我們可以用
Java Applet 或 ActiveX 這兩種技術規格來設計監控程式。在本範例中,我們採用了
ActiveX + VBScript 來實作。至於 Java Applet 者,則留待您自行練習。
首先,我們必須將 iPush ActiveX API component (iPushX.ocx)
的存取語法加入步驟七完成的網頁中。
請用記事本編輯器開啟 c:\ipushdemo\ipush_factory.html,在檔案最後
(</html> 之後) 加入以下 HTML 原始碼 (您可以 Copy-Paste 複製之):
<object classid="clsid:455A78EC-7EB0-4483-ABD9-2AAB10A1644B"
codebase="file://c:\ipushdemo/iPushX.ocx"
id=ipushx>
<param name="ipuship" value="192.168.0.15">
<param name="ipushport" value=8000>
<param name="company" value="ICE">
<param name="product" value="ICE">
<param name="username" value="monitor">
<param name="password" value="monitor">
</object>
此原始碼載入並註冊 c:\ipushdemo\iPushX.ocx
元件,將其命名為 ipushx,並將連接
iPush Server (範例位於 IP: 192.168.0.15,請依您實際的主機位址改變之) 所需的 Server
與 User 帳號參數傳遞給此 iPush ActiveX API component。
請注意,若 iPushX.ocx 位於 Web Server 上,可以使用類似
codebase="http://192.168.0.15/ipushdemo/iPushX.ocx"
等指令來下載,當然,該路徑必須是可讓 Web Server 存取者。
接下來,我們必須下指令來驅動此 iPush ActiveX API component,要求它與
iPush Server 連線,訂閱相關頻道,並處理所接收的即時訊息,VBScript 原始碼如下:
<SCRIPT LANGUAGE=VBScript>
ipushx.ipushConnect
Sub ipushx_ConnectReady(iPushIP, iPushPort)
window.status = "connection ready:" & iPushIP
ipushx.ipushSub "ch01"
ipushx.ipushSub "ch02"
ipushx.ipushSub "ch03"
ipushx.ipushSub "ch04"
ipushx.ipushSub "ch05"
End Sub
Sub ipushx_ConnectFail(nStatus)
window.status = "connect failed " & nStatus
End Sub
Sub ipushx_ConnectLost()
window.status = "connection lost"
End Sub
Sub ipushx_CommandMsg(nCode, strMsg)
window.status = "Command Message: " & nCode & strMsg
End Sub
Sub ipushx_DataReceived(channel, data)
data1 = data /10
if channel="ch01" then
form1.textfield.value = data1 & " kg/cm"
elseif channel="ch02" then
form1.textfield2.value = data1 & " kg/cm"
elseif channel="ch03" then
form1.textfield3.value = data1 & " ℃"
elseif channel="ch04" then
form1.textfield4.value = data1 & " ℃"
elseif channel="ch05" then
form1.textfield5.value = data1 & " ℃"
end if
End Sub
</SCRIPT>
將此 VBScript 原始碼接續放在 ipush_factory.html 的最後並儲存。至此,我們的
ActiveX 監控程式已經大功告成。這一段 VBScript 做了下列動作:
- <SCRIPT LANGUAGE=VBScript>
表示以下的原始碼為 VBScript。
- ipushx.ipushConnect
指示 iPush ActiveX API component (名為 ipushx) 與 iPush Server
連線,連線所需的參數,先前已經透過 <param> 標籤告訴 iPush ActiveX API component
了。
- 當與 iPush Server 連線成功後,iPush ActiveX API
component 會自動呼叫名為 ipushx_ConnectReady(iPushIP,
iPushPort) 的函式,我們在此函式內加入向 iPush Server 訂閱頻道的指令,當與
iPush Server 連線成功時,使用指令 ipushx.ipushSub
訂閱頻道“ch01”~“ch05”。
- 若與 iPush Server 連線失敗或斷線,iPush ActiveX
API component 會自動呼叫 ipushx_ConnectFail()、ipushx_ConnectLost()、與
ipushx_CommandMsg() 函式,使用者可以在這些函式內加入如何對應處理的程序。
- 最後,當訂閱的頻道 (“ch01”~“ch05”) 有訊息從 iPush
Server 推播過來時,iPush ActiveX API component 會自動呼叫
ipushx_DataReceived(channel, data) 函式,並傳入頻道名稱與訊息內容。我們在此函式內加上一些簡單的處理
─ 把 Data 除以 10,加上單位,並將其顯示在相對的欄位,欄位顯示對應如下:
- 頻道 ch01 的資料:form1.textfield.value
- 頻道 ch02 的資料:form1.textfield2.value
- 頻道 ch03 的資料:form1.textfield3.value
- 頻道 ch04 的資料:form1.textfield4.value
- 頻道 ch05 的資料:form1.textfield5.value
->
嫌分段編輯 HTML 檔案太累?好吧,圖方便的您,可以直接下載這整個 ipush_factory.html 檔案回去。下載存檔時,記得將附檔名從
.txt 改成 .html。<
Download: ipush_factory.txt
> (3 KB) |