硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2362|回复: 3
收起左侧

[RL-TCPnet教程] 【RL-TCPnet网络教程】第51章 RL-TCPnet中Ajax使用方法

[复制链接]

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
发表于 2018-1-16 16:42:18 | 显示全部楼层 |阅读模式
第51章      RL-TCPnet中Ajax使用方法

      本章节为大家讲解RL-TCPnet中Ajax的实现方法,特别是具体的执行过程说明。关于这点非常有必要讲解下,否则后面学习了Ajax也是云里雾里。
51.1 初学者重要提示
51.2 为什么使用Ajax及其优势
51.3 Ajax执行流程举例
51.4 官方手册中做的举例
51.5      总结

51.1  初学者重要提示
      学习本章节前,请务必保证已经学习了第48,49和50章,这三章的基础知识非常重要,否则学习本章节将比较吃力。

51.2  为什么使用Ajax及其优势
       使用RL-TCPnet脚本语言将动态数据传递给JavaScript对象,可以让用户轻松构建复杂的HTML页面。但是,如果用户尝试显示频繁更改的数据就非常不方便了。一个简单的解决方案是让用户按浏览器的刷新按钮或在HTML代码的<head>部分添加刷新标签。
  1. <meta http-equiv = "refresh" content="600">
复制代码

      虽然这样做可以解决,但是这种方法有两个问题:
1、首先,由于这种方法需要重新加载整个页面,会导致屏幕闪烁,用户体验差。
2、其次,浏览器必须再次下载整个页面,速度很慢并且消耗带宽。
     解决这个问题的方法是使用第50章为大家讲解的Ajax(异步JavaScript+XML)。Ajax使用XML管理需要动态更新的数据,然后将该数据以HTML代码中设置的更新率定时发给浏览器。
      使用Ajax有如下优势:
1、动态应用数据以单个TCP数据包发送,提供非常快的更新速率且消耗带宽小。
2、浏览器可以使用XML更新网页,而无需重新加载整个页面,如此就实现了网页中动态对象无闪烁的、实时的更新。
3、由于Ajax技术是将页面处理从Web服务器移动到本地计算机,JavaScript脚本在本地计算机上处理网页并生成网页更新,Web服务器的负担小,可以同时处理更多的客户端。

努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2018-1-16 16:48:30 | 显示全部楼层
51.3 Ajax执行流程举例
      为了帮助大家更好的了解Ajax在RL-TCPnet中的工作原理,我们这里举一个例子,创建一个如下所示的控制界面。8个复选框用来反馈板子上8个实体按键的状态。“Refresh”按钮用于手动刷新8个复选框的状态,而“Periodic”复选框用于开启周期性更新8个复选框的状态。
51.1.png
                              
      使用Ajax需要用户添加官方做好的JavaScript文件xml_http.js(以MDK4.74为例,在MDK安装目录的C:\Keil_v474\ARM\RL\TCPnet\Java文件里面)。XML管理的动态更新数据也要单独创建一个文件。XML文件的后缀名是.cgx,跟CGI脚本文本的后缀cgi要区分开。
51.2.png
      首先来看buttons.cgi文件的代码,重点看标签<script></script>之间的JavaScript函数和标签<form></form>之间的输入表单。
  1. #------------------------------------------------

  2. # HTML script in buttons.cgi

  3. #------------------------------------------------

  4. t <html><head><title>Button inputs</title>

  5. t <script language=JavaScript type="text/javascript" src="xml_http.js"></script>

  6. t <script language=JavaScript type="text/javascript">

  7. # Define URL and refresh timeout

  8. t var formUpdate = new periodicObj("buttons.cgx", 300);

  9. t function periodicUpdate() {

  10. t  if(document.getElementById("refreshChkBox").checked == true) {

  11. t   updateMultiple(formUpdate);

  12. t   periodicFormTime = setTimeout("periodicUpdate()", formUpdate.period);

  13. t  }

  14. t  else

  15. t   clearTimeout(periodicFormTime);

  16. t }

  17. t </script></head>

  18. i pg_header.inc

  19. t <h3 align="center"><br>Buttons on the board</h3>

  20. t <p><font size="2">This page allows you to monitor on board buttons state.

  21. t  Periodic screen update is based on <b>xml</b> technology. This results in smooth

  22. t  flicker-free screen update.<br><br>

  23. t  Press a button on an evaluation board and observe the change on the screen.</font></p>

  24. t <form action="buttons.cgi" method="post" id="form1" name="form1">

  25. t <table border="0" width=99%><font size="3">

  26. t <tr bgcolor=#aaccff>

  27. t  <th width=40%>Item</th>

  28. t  <th width=60%>Status</th>

  29. t </tr>

  30. t <tr>

  31. t  <td><img src="pabb.gif">Buttons [7..0]:</td>

  32. t  <td align="center">

  33. t   <input type="checkbox" disabled id="button7">7

  34. t   <input type="checkbox" disabled id="button6">6

  35. t   <input type="checkbox" disabled id="button5">5

  36. t   <input type="checkbox" disabled id="button4">4

  37. t   <input type="checkbox" disabled id="button3">3

  38. t   <input type="checkbox" disabled id="button2">2

  39. t   <input type="checkbox" disabled id="button1">1

  40. t   <input type="checkbox" disabled id="button0">0

  41. t  </td>

  42. t </tr>

  43. t </font></table>

  44. t <p align="center">

  45. t  <input type="button" id="refreshBtn" value="Refresh" onclick="updateMultiple(formUpdate)">

  46. t  Periodic:<input type="checkbox" id="refreshChkBox" onclick="periodicUpdate()">

  47. t </p></form>

  48. i pg_footer.inc

  49. . End of script must be closed with period.
复制代码
从上面的输入表单中可以看出:
(1)创建了8个复选框checkbox。
(2)一个刷新按钮button。
(3)还有一个控制周期刷新的复选框checkbox。

51.3.1 点击按钮实现的单次更新过程
      当用户在浏览器上按下按钮button时,会触发buttons.cgi文件中设置的按钮触发事件onclick=“updateMultiple(formUpdate)”,从而浏览器会从服务器下载JavaScript函数updateMultiple()并执行,函数updateMultiple是在xml_http.js文件中实现:
  1. /*----------------------------------------

  2. * xml_http.js

  3. *---------------------------------------*/

  4. function updateMultiple(formUpd, callBack, userName, userPassword) {

  5. xmlHttp = GetXmlHttpObject();

  6. if(xmlHttp == null) {

  7.   alert("XmlHttp not initialized!");

  8.   return 0;

  9. }

  10. xmlHttp.onreadystatechange = responseHandler;

  11. xmlHttp.open("GET", formUpd.url, true, userName, userPassword);

  12. xmlHttp.send(null);



  13. function responseHandler(){

  14.   if(xmlHttp.readyState == 4) { //response ready

  15.    if(xmlHttp.status == 200) { //handle received data

  16.     var xmlDoc = xmlHttp.responseXML;

  17.     if(xmlDoc == null)

  18.      return 0;

  19.     try {  //catching IE bug

  20.      processResponse(xmlDoc);

  21.     }

  22.     catch(e) {

  23.      return 0;

  24.     }

  25.     /* Callback function for custom update. */

  26.     if (callBack != undefined)

  27.      callBack();

  28.    }

  29.    else if(xmlHttp.status == 401)

  30.     alert("Error code 401: Unauthorized");

  31.    else if(xmlHttp.status == 403)

  32.     alert("Error code 403: Forbidden");

  33.    else if(xmlHttp.status == 404)

  34.     alert("Error code 404: URL not found!");

  35.   }

  36. }

  37. }
复制代码
函数updateMultiple的形参是formUpdate,这个是在buttons.cgi文件中创建的JavaScript对象:
  1. #--------------------------------------

  2. # buttons.cgi

  3. #--------------------------------------

  4. t var formUpdate = new periodicObj ("buttons.cgx", 300);
复制代码
而函数periodicObj也是在xml_http.js中实现,定义了周期性更新的文件,时间单位ms。这里是浏览器300ms后下载一次buttons.cgx文件进行更新。
  1. /* Objects templates */

  2. function periodicObj(url, period) {

  3. this.url = url;

  4. this.period = (typeof period == "undefined") ? 0 : period;

  5. }
复制代码
buttons.cgx文件的实现如下。
  1. #--------------------------------------

  2. # Buttons.cgx

  3. #--------------------------------------

  4. t <form>

  5. c y0

  6. c y1

  7. c y2

  8. c y3

  9. c y4

  10. c y5

  11. c y6

  12. c y7

  13. t </form>
复制代码
对于XML文件来说,仅写入要更新的元素即可,并以标签<form>开始,标签</form>结束。这个XML文件里面也有CGI脚本命令,会触发HTTP_CGI.c文件中的脚本解析函数cgi_func的执行:
  1. /*---------------------------------------

  2. * cgi_func() in HTTP_CGI.c

  3. *--------------------------------------*/

  4. case 'y':

  5. len = sprintf (

  6. (char*) buf,

  7. "<checkbox><id><button%c</id> <on>%s</on></checkbox>",

  8. env [1],

  9. (get_button () & (1<<(env [1]-'0'))) ? "true" : "false"

  10. );

  11. break;
复制代码
几条脚本命令全部解析后,就会返回8个checkbox的状态给浏览器,即buttons.cgx文件中的代码会全部传给浏览器进行更新。
  1. <!-----------------------------------------

  2. # Generated XML

  3. #----------------------------------------->

  4. <form>

  5. <checkbox><id>button0</id><on>true</on></checkbox>

  6. <checkbox><id>button1</id><on>false</on></checkbox>

  7. <checkbox><id>button2</id><on>false</on></checkbox>

  8. <checkbox><id>button3</id><on>false</on></checkbox>

  9. <checkbox><id>button4</id><on>false</on></checkbox>

  10. <checkbox><id>button5</id><on>false</on></checkbox>

  11. <checkbox><id>button6</id><on>false</on></checkbox>

  12. <checkbox><id>button7</id><on>false</on></checkbox>

  13. </form>
复制代码
特别注意:
1、XML文件buttons.cgx中的每行语句不可超过120个字符。
2、函数cgi_func返回的XML代码中不能有空格和回车换行符。
3、XML文件的后缀务必要是cgx,防止WebServer的脚本解析器无法正确识别。

51.3.2 选中复选框后的周期性更新过程
      如果用户选中了定期更新复选框,浏览器将调用函数periodicUpdate()并执行。
  1. #--------------------------------------

  2. # buttons.cgi

  3. #--------------------------------------

  4. t function periodicUpdate() {

  5. t if (document.getElementById ("refreshChkBox").checked == true) {

  6. t updateMultiple (formUpdate);

  7. t periodicFormTime = setTimeout ("periodicUpdate ()",formUpdate.period);

  8. t } else {

  9. t clearTimeout (periodicFormTime);

  10. t }

  11. t }
复制代码

1、这个JavaScript函数在buttons.cgi文件中的<head>标签里面,主要功能是通过判断复选框是否选中了,如果选中了,将调用函数updateMultiple,更新8个复选框的状态并返回给浏览器。并通过函数setTimeout启动了一个单次定时器,设置时间为formUpdate.period,单位毫秒。时间到后会再次调用函数periodicUpdate,从而实现了周期更新的效果。
2、如果复选框取消选中或者未选中,调用函数clearTimeout停止开启的单次定时器。
3、关于JavaScript的定时器函数说明,看此贴:http://bbs.armfly.com/read.php?tid=36101

       至此,通过Ajax就实现了网页的局部刷新,实际效果极佳。

努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2018-1-16 16:50:17 | 显示全部楼层
51.4 官方手册中做的举例
       第 3 章 3.4 小节里面说的参考资料 rlarm.chm 文件里面也有一个Ajax的举例说明,可以参考学习:
51.3.png

努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2018-1-16 16:50:37 | 显示全部楼层
51.5   总结
            关于本章节的知识点还是比较重要的,需要初学者务必掌握,下一章节将进行实战。

努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 22:00 , Processed in 0.165786 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表