Robotics StackExchange | Archived questions

roslibjs auto disconnected when calling any function

I'm running melodic on macOS 10.13.6.

I got the latest rosauth and rosbridgesuite from git and have them in my catkinws and build ok. I also have my test project which launches the rosbridge server and a simple python web server for the webpage.

Here's the javascript snippet that is used to connect:

$(function() {
  var ros = new ROSLIB.Ros({
    url : 'ws://127.0.0.1:9090'
  });
  window.ros = ros;

  ros.on('connection', function() {
    console.log('Connected to websocket server.');
    $('#status span').html('Connected');
  });

  ros.on('error', function(error) {
    console.log('Error connecting to websocket server: ', error);
    $('#status span').html('Error');
  });

  ros.on('close', function() {
    console.log('Connection to websocket server closed.');
    $('#status span').html('Disconnected');
  });

And here's the console output:

Connected to websocket server.
ros.getNodes(function(n){console.log(n)});
undefined
main.js:18 Connection to websocket server closed.
ros.connect('ws://127.0.0.1:9090')
undefined
main.js:8 Connected to websocket server.
ros
d {socket: WebSocket, idCounter: 1, isConnected: true, transportLibrary: "websocket", transportOptions: {…}, …}event: "connection"groovyCompatibility: trueidCounter: 1isConnected: truesocket: WebSocket {url: "ws://127.0.0.1:9090/", readyState: 1, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}transportLibrary: "websocket"transportOptions: {}_conf: {maxListeners: 0}_events: {maxListeners: 0, connection: ƒ, error: ƒ, close: ƒ, call_service:/rosapi/nodes:1: ƒ}__proto__: g
ros.getNodes(function(n){console.log(n)});
undefined
main.js:18 Connection to websocket server closed.
ros
d {socket: WebSocket, idCounter: 2, isConnected: false, transportLibrary: "websocket", transportOptions: {…}, …}event: "close"groovyCompatibility: trueidCounter: 2isConnected: falsesocket: WebSocket {url: "ws://127.0.0.1:9090/", readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}transportLibrary: "websocket"transportOptions: {}_conf: {maxListeners: 0}_events: {maxListeners: 0, connection: ƒ, error: ƒ, close: ƒ, call_service:/rosapi/nodes:1: ƒ, …}__proto__: g

Here't the output from the terminal window

[INFO] [1542421699.375919]: Serving at 127.0.0.1:8080
registered capabilities (classes):
 - rosbridge_library.capabilities.call_service.CallService
 - rosbridge_library.capabilities.advertise.Advertise
 - rosbridge_library.capabilities.publish.Publish
 - rosbridge_library.capabilities.subscribe.Subscribe
 - <class 'rosbridge_library.capabilities.defragmentation.Defragment'>
 - rosbridge_library.capabilities.advertise_service.AdvertiseService
 - rosbridge_library.capabilities.service_response.ServiceResponse
 - rosbridge_library.capabilities.unadvertise_service.UnadvertiseService
[INFO] [1542421699.610645]: Rosbridge WebSocket server started on port 9090
127.0.0.1 - - [16/Nov/2018 21:28:37] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:28:37] "GET /main.js HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:28:38] code 404, message File not found
127.0.0.1 - - [16/Nov/2018 21:28:38] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [16/Nov/2018 21:29:25] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:33:34] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:33:34] "GET /main.js HTTP/1.1" 200 -
[INFO] [1542422015.325308]: Client connected.  1 clients total.
[INFO] [1542422020.249735]: Client disconnected. 0 clients total.
127.0.0.1 - - [16/Nov/2018 21:34:18] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:34:19] "GET /main.js HTTP/1.1" 200 -
[INFO] [1542422059.736674]: Client connected.  1 clients total.
[INFO] [1542422077.388880]: Client disconnected. 0 clients total.
127.0.0.1 - - [16/Nov/2018 21:34:57] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:34:57] "GET /main.js HTTP/1.1" 200 -
[INFO] [1542422098.519738]: Client connected.  1 clients total.
127.0.0.1 - - [16/Nov/2018 21:39:40] "GET / HTTP/1.1" 200 -
[INFO] [1542422380.567272]: Client disconnected. 0 clients total.
127.0.0.1 - - [16/Nov/2018 21:39:40] "GET /main.js HTTP/1.1" 200 -
[INFO] [1542422381.380018]: Client connected.  1 clients total.
[INFO] [1542422386.657175]: Client disconnected. 0 clients total.
127.0.0.1 - - [16/Nov/2018 21:41:34] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Nov/2018 21:41:34] "GET /main.js HTTP/1.1" 200 -
[INFO] [1542422495.102058]: Client connected.  1 clients total.
[INFO] [1542422539.985192]: Client disconnected. 0 clients total.
[INFO] [1542422906.580086]: Client connected.  1 clients total.
[INFO] [1542422919.389533]: Client disconnected. 0 clients total.
[INFO] [1542422936.279464]: Client connected.  1 clients total.
[INFO] [1542423000.416417]: Client disconnected. 0 clients total.

Here I'm using the getNodes function, but any function that tries to interact with the server leads to the same result.

I have also gotten the same code to work on a ubuntu vm running kinetic. (Slight changes to the url and adding the necessary packages to the catkin ws)

Asked by kdorsel on 2018-11-16 22:13:01 UTC

Comments

Am experiencing this same issue. Have you found any resolution?

Asked by dtf on 2018-12-05 01:50:33 UTC

Answers

If you're using rosauth with rosbridge you'll need to authenticate after connection, otherwise the server will close the connection. See http://robotwebtools.org/jsdoc/roslibjs/current/Ros.html

authenticate(mac, client, dest, rand, t, level, end)
Sends an authorization request to the server.
Parameters:
Name    Type    Description
mac         MAC (hash) string given by the trusted source.
client      IP of the client.
dest        IP of the destination.
rand        Random string given by the trusted source.
t       Time of the authorization request.
level       User level as a string given by the client.
end         End time of the client's session.

Asked by pROScription on 2019-06-20 01:04:56 UTC

Comments