下面的例子顯示了你想完成什麼的一種方式。通過實驗,您至少可以感受到它在工作時的感受。
+---------+ +---------+ +---------+
| Receive | | Receive | | Send |
| Browser | | Browser | | Browser |
+----+----+ +----+----+ +----+----+
| | |
| | |
+-------+------+--------------+ +---------+
| | telnet |
| +--------------------------+ CLI |
| | | session |
| | +---------+
+--+---+--+
| Accord | +------------------------+
| Cloud +----+ C/Java/Perl/Python etc |
| Service | | Program Language APIs |
+---------+ +------------------------+
有幾種方法可以在瀏覽器和Web服務之間建立雙向通信通道。例如。 WebSocket,AJAX等
在以下示例中,單擊發送按鈕時,下面的發送瀏覽器發送輸入的文本。
![enter image description here](https://i.stack.imgur.com/lM5Gw.jpg)
當接收瀏覽器收到通知時,它更新的瀏覽器內容與計數器值和新文本字符串。它在每次收到更新時遞增計數器。
![enter image description here](https://i.stack.imgur.com/uneBd.jpg)
在下面send.html和receive.html代碼,Accord.js建立一個瀏覽器和雅閣雲服務之間的通信信道。發送和接收瀏覽器使用ActiveML與Accord Cloud Service進行交互,這是JSON和XML元語言的混合體。
prompt> cat send.html
<html>
<head>
<title>Accord Software, Inc.</title>
<link rel="icon" href="/favicon.gif"/>
</head>
<body>
<script type="text/javascript" src="http://ac.accord.com/src/Accord.js"></script>
<script type="text/javascript">
var rpc;
function run() {
if (typeof AccordAmlHttpRpc != 'function' ||
typeof checkSessionId != 'function') {
setTimeout(function(){run();}, 100);
return;
}
rpc = new AccordAmlHttpRpc();
}
/*
* Send the text string when 'Click to Send' button is acted upon.
* This ActiveML command will update the string value and any
* sessions that have outstanding 'wait for an update' will unblock
* and receive the update notification.
*/
function sendMessage() {
var elem = document.getElementById("SendMsg");
rpc.call('aml set string Demo.Msg = "' + elem.value + '";');
}
run();
</script>
<br>
Enter text:
<input id="SendMsg" type="text" value="" maxlength="50" />
<button onclick="sendMessage()">Click to Send</button>
</body>
</html>
prompt> cat recv.html
<html>
<head>
<title>Accord Software, Inc.</title>
<link rel="icon" href="/favicon.gif"/>
</head>
<body>
<div id="Page"></div>
<script type="text/javascript" src="http://ac.accord.com/src/Accord.js"></script>
<script type="text/javascript">
var rpc;
var div = document.getElementById('Page');
/*
* Display the string and increment counter.
*/
var count = 0;
function DisplayMsg(s) {
div.innerHTML = count + ': ' + s;
count++;
}
/*
* Event is received as 'ActiveML set string Demo.Msg = "hello, world";'
*/
function RecvMsg(s) {
var eq = s.indexOf(' = ');
/*
* Remove quotes and semico at the end.
*/
s = s.substring(eq+4, s.length-2);
DisplayMsg(s);
}
/*
* DisplayString() is called initially to display the current value
* followed by RecvMsg() for each subsequent update.
*/
function run() {
if (typeof AccordAmlHttpRpc != 'function' ||
typeof checkSessionId != 'function') {
setTimeout(function(){run();}, 100);
return;
}
rpc = new AccordAmlHttpRpc();
/*
* Communication with the back-end service by using
* ActiveML.
*/
rpc.call('aml print string Demo.Msg;', DisplayMsg, RecvMsg);
rpc.call('aml wait for an update to print string Demo.Msg;', 0, 0);
}
run();
</script>
</body>
</html>
爲了使瀏覽器與Accord Cloud Service進行通信,需要從每個瀏覽器登錄。您可以通過點擊ac.accord.com上的登錄按鈕來創建一個臨時免費帳戶來嘗試。創建帳戶後,您需要telnet到ac.accord.com並在執行任何「發送」或「接收」之前執行以下操作。在Windows下載並使用PuTTY。在linux/bsd上使用telnet。
prompt> telnet ac.accord.com
Connected to ac.accord.com.
Escape character is '^]'.
Accord ActiveML - Version 1.0.0.0
Copyright (c) 2001-2013, Accord Software, Inc. All rights reserved.
ActiveML Uid: <email>
Password: <password>
Welcome !
aml> create aobject Demo;
aml> create string Demo.Msg;
aml> set string Demo.Msg = "hello, world";
每一組命令被從任一個「發送」的瀏覽器或通過telnet CLI接收到的時間,所述「接收」的瀏覽器將更新其顯示。
除了使用Telnet命令行模式下,你還可以與雅閣雲服務使用不同的編程語言如C/C++,Java和Perl,Python和等互動
如果沒有完成這個任務,預算,基於訂閱的解決方案可能值得評估。訂購基於雲的解決方案可能是一種具有成本效益的解決方案。 (有時它可能比你花在咖啡上的花費少!)。披露:我爲Accord工作。
你看過藍牙嗎?你能根植你的設備來實現嗎? – minhaz