ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

<html> <head> <script type="text/javascript" src="http://brown-ros-pkg.googlecode.com/svn/tags/brown-ros-pkg/rosbridge/ros.js"></script> <script type="text/javascript"> function nop() {} /* console for logging */ var console = null;

function pub()
{ 
    var connection2 = new ros.Connection("ws://127.0.0.1:9090");
    connection2.setOnOpen(function (e) {
        log('connected to ros for 2nd time');
        log('attempting to send message over 2nd socket connection.');
    connection2.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Pub Button Click Message"' + '}');

    });
}

function displayDate()
{
document.getElementById("demo").innerHTML=Date();
var connection2 = new ros.Connection("ws://127.0.0.1:9090");
connection2.setOnOpen(function (e) {
    log('connected to ros for 2nd time');
    log('attempting to send message over 2nd socket connection.');
connection2.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Display Date Button"' + '}');

});
}

function log(msg) {
    console.innerHTML = console.innerHTML + msg + "<br/>";
}
function init() {
    function waitForDOM() {
        var cnsl = document.getElementById('console');
        if (cnsl == null) {
            setTimeout(waitForDOM, 100);
        } else {
            console = cnsl;
            setTimeout(main, 0);
        }
    }
    setTimeout(waitForDOM, 100);
}

function main() {
    log('console initialized');
    var connectInfo = document.location.toString();
    log('url: ' + connectInfo);
    var addressMatches = connectInfo.match(/address=([^&]*)/);
    if (addressMatches == null) {
        log('Problem extracting address!');
        return;
    }
    var address = addressMatches[1];
    log('address: ' + address);
    var portMatches = connectInfo.match(/.*&port=([^&]*)/);
    if (portMatches == null) {
        log('Problem extracting port!');
        return;
    }
    var port = portMatches[1];
    log('port: ' + port);
    log('creating ROSProxy connection object...');
    var connection = null;
    try {
        connection = new ros.Connection('ws://' + address + ':' + port);
    } catch (err) {
        log('Problem creating proxy connection object!');
        return;
    }
    log('created');
    log('connecting to ' + address + ' on port ' + port + '...');

    connection.setOnClose(function (e) {
        log('connection closed');
    });
    connection.setOnError(function (e) {
        log('network error!');
    });
    connection.setOnOpen(function (e) {
        log('connected');
        log('initializing ROSProxy...');
        try {
            connection.callService('/rosjs/topics', '[]', nop);
        } catch (error) {
            log('Problem initializing ROSProxy!');
            return;
        }
        log('initialized');       
        log('running');  
        log('trying to publish a message');
        connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Page Load Message"' + '}');
    });
}
    </script>
  </head>
<body onload="init()">
<div id="console"></div>
<h1>Rosbridge Test Page</h1>

This page shows how to publish messages with json over rosbridge on page load and on button click event

<button type="button" onclick="displayDate()">Display Date</button> <button type="button" onclick="pub()">Publish String Message</button> </body> </html>

<html> <head> <html> <head> <script type="text/javascript" src="http://brown-ros-pkg.googlecode.com/svn/tags/brown-ros-pkg/rosbridge/ros.js"></script> src="http://brown-ros-pkg.googlecode.com/svn/tags/brown-ros-pkg/rosbridge/ros.js"></script> <script type="text/javascript"> type="text/javascript"> function nop() {} {} /* console for logging */ */ var console = null;

function pub() { { var connection2 = new ros.Connection("ws://127.0.0.1:9090"); connection2.setOnOpen(function (e) { log('connected to ros for 2nd time'); log('attempting to send message over 2nd socket connection.'); connection2.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Pub Button Click Message"' + '}'); '}');

});
}

}

function displayDate() { document.getElementById("demo").innerHTML=Date(); var connection2 connection3 = new ros.Connection("ws://127.0.0.1:9090"); connection2.setOnOpen(function connection3.setOnOpen(function (e) { log('connected to ros for 2nd 3rd time'); log('attempting to send message over 2nd 3rd socket connection.'); connection2.publish('/recognizer/output', connection3.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Display Date Button"' + '}'); '}');

}); } }

function log(msg) { console.innerHTML = console.innerHTML + msg + "<br/>"; "
";
} function init() { function waitForDOM() { var cnsl = document.getElementById('console'); if (cnsl == null) { setTimeout(waitForDOM, 100); } else { console = cnsl; setTimeout(main, 0); } } setTimeout(waitForDOM, 100); } }

function main() { log('console initialized'); var connectInfo = document.location.toString(); log('url: ' + connectInfo); var addressMatches = connectInfo.match(/address=([^&]*)/); if (addressMatches == null) { log('Problem extracting address!'); return; } var address = addressMatches[1]; log('address: ' + address); var portMatches = connectInfo.match(/.*&port=([^&]*)/); if (portMatches == null) { log('Problem extracting port!'); return; } var port = portMatches[1]; log('port: ' + port); document.location.toString();

log('creating ROSProxy connection object...');
 var connection = null;
 try {
     connection = new ros.Connection('ws://' + address + ':' + port);
    ros.Connection("ws://127.0.0.1:9090");
} catch (err) {
     log('Problem creating proxy connection object!');
     return;
 }
    log('created');
    log('connecting to ' + address + ' on port ' + port + '...');

    log('connection created');

connection.setOnClose(function (e) {
     log('connection closed');
 });
 connection.setOnError(function (e) {
     log('network error!');
 });
 connection.setOnOpen(function (e) {
     log('connected');
     log('initializing ROSProxy...');
     try {
         connection.callService('/rosjs/topics', '[]', nop);
     } catch (error) {
         log('Problem initializing ROSProxy!');
         return;
     }
     log('initialized');       
     log('running');  
     log('trying to publish a message');
     connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Page Load Message"' + '}');
 });

} </script> </head> <body onload="init()"> <div id="console"></div> <h1>Rosbridge onload="init()">

Rosbridge Test Page</h1>

This Page

<p id="demo">This page shows how to publish messages with json over rosbridge on page load and on button click event

event</p>

<button type="button" onclick="displayDate()">Display Date</button> <button type="button" onclick="pub()">Publish String Message</button> </body> Message</button> </body> </html>

<html> <head> <script type="text/javascript" src="http://brown-ros-pkg.googlecode.com/svn/tags/brown-ros-pkg/rosbridge/ros.js"></script> <script type="text/javascript"> function nop() {} /* console for logging */ var console = null;

function pub()
{
    var connection2 = new ros.Connection("ws://127.0.0.1:9090");
    connection2.setOnOpen(function (e) {
        log('connected to ros for 2nd time');
        log('attempting to send message over 2nd socket connection.');
    connection2.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Pub Button Click Message"' + '}');

'}');

    });

}

} function displayDate() { document.getElementById("demo").innerHTML=Date(); var connection3 = new ros.Connection("ws://127.0.0.1:9090"); connection3.setOnOpen(function (e) { log('connected to ros for 3rd time'); log('attempting to send message over 3rd socket connection.'); connection3.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Display Date Button"' + '}');

'}'); }); }

} function log(msg) { console.innerHTML = console.innerHTML + msg + "
";
"<br/>"; } function init() { function waitForDOM() { var cnsl = document.getElementById('console'); if (cnsl == null) { setTimeout(waitForDOM, 100); } else { console = cnsl; setTimeout(main, 0); } } setTimeout(waitForDOM, 100); }

} function main() { log('console initialized'); var connectInfo = document.location.toString();

document.location.toString();

    log('creating ROSProxy connection object...');
 var connection = null;
 try {
     connection = new ros.Connection("ws://127.0.0.1:9090");
 } catch (err) {
     log('Problem creating proxy connection object!');
     return;
}
    }
    log('connection created');

 connection.setOnClose(function (e) {
     log('connection closed');
 });
 connection.setOnError(function (e) {
     log('network error!');
 });
 connection.setOnOpen(function (e) {
     log('connected');
     log('initializing ROSProxy...');
     try {
         connection.callService('/rosjs/topics', '[]', nop);
     } catch (error) {
         log('Problem initializing ROSProxy!');
         return;
    }
    }
        log('initialized');       
     log('running');  
     log('trying to publish a message');
     connection.publish('/recognizer/output', 'std_msgs/String', '{"data":' + '"On Page Load Message"' + '}');
 });

} </script> </head> <body onload="init()">

Rosbridge onload="init()"> <div id="console"></div> <h1>Rosbridge Test Page

Page</h1>

    <p id="demo">This page shows how to publish messages with json over rosbridge on page load and on button click event</p>

<button type="button" onclick="displayDate()">Display Date</button> <button type="button" onclick="pub()">Publish String Message</button> </body> </html>

</html>