Advertisement
Guest User

Rosbridge Test Page: Single Connection

a guest
Feb 20th, 2012
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.55 KB | None | 0 0
  1. <html>
  2.   <head>
  3.     <script type="text/javascript" src="http://brown-ros-pkg.googlecode.com/svn/tags/brown-ros-pkg/rosbridge/ros.js"></script>
  4.     <script type="text/javascript">
  5. function nop() {} /* console for logging */
  6. var console = null;
  7. var connection = null;
  8.  
  9. function pub() {
  10.   if (connection == null) return;
  11.   log('attempting to send message over 1st socket connection.');
  12.   connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Pub Button Click Message"' + '}');
  13. }
  14.  
  15. function displayDate() {
  16.   document.getElementById("demo").innerHTML=Date();
  17.   if (connection == null) return;
  18.   log('attempting to send message over 1st socket connection.');
  19.   connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Display Date Button"' + '}');
  20. }
  21.  
  22. function log(msg) {
  23.   if (console == null) return;
  24.   console.innerHTML = console.innerHTML + msg + "<br/>";
  25. }
  26.  
  27. function init() {
  28.   function waitForDOM() {
  29.     var cnsl = document.getElementById('console');
  30.     if (cnsl == null) {
  31.       setTimeout(waitForDOM, 100);
  32.     } else {
  33.       console = cnsl;
  34.       setTimeout(main, 0);
  35.     }
  36.   }
  37.   setTimeout(waitForDOM, 100);
  38. }
  39.  
  40. function main() {
  41.   log('console initialized');
  42.   var connectInfo = document.location.toString();
  43.  
  44.   log('creating ROSProxy connection object...');
  45.  
  46.   try {
  47.     connection = new ros.Connection("ws://127.0.0.1:9090");
  48.   } catch (err) {
  49.     log('Problem creating proxy connection object!');
  50.     return;
  51.   }
  52.  
  53.   log('connection created');
  54.  
  55.   connection.setOnClose(function (e) {
  56.     log('connection closed');
  57.   });
  58.   connection.setOnError(function (e) {
  59.     log('network error!');
  60.   });
  61.   connection.setOnOpen(function (e) {
  62.     log('connected');
  63.     log('initializing ROSProxy...');
  64.     try {
  65.       connection.callService('/rosjs/topics', '[]', nop);
  66.     } catch (error) {
  67.       log('Problem initializing ROSProxy!');
  68.       return;
  69.     }
  70.  
  71.     log('initialized');
  72.     log('running');
  73.     log('trying to publish a message');
  74.  
  75.     connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Page Load Message"' + '}');
  76.   });
  77. }
  78.     </script>
  79.   </head>
  80. <body onload="init()">
  81. Rosbridge Test Page
  82. <p id="demo">This page shows how to publish messages with json over rosbridge on page load and on button click event</p>
  83. <button type="button" onclick="displayDate()">Display Date</button>
  84. <button type="button" onclick="pub()">Publish String Message</button><br/><br/>
  85. <b>Log:</b><div id="console" style="margin-left: 1em;"></div>
  86. </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement