Search code examples
websocketros

Can't establish connection to Web Server using rosbridge


I have created a simple HTML page to control the movement of a simulated Gazebo Turtlebot using roslaunch rosbridge_server rosbridge_websocket.launch following this tutorial.

However, in the Web Console of the HTML page (F12) it shows the error "Firefox cant establish a connection to the server at ws://localhost:9090/." I am using the default rosbridge for the websocket(9090). In the Terminal I am also receiving the errors:

[-] failing WebSocket opening handshake ('WebSocket connection denied: origin 'null' not allowed')

[-] dropping connection to peer tcp4:127.0.0.1:41290 with abort=False: WebSocket connection denied: origin 'null' not allowed.

Does anyone have any suggestions on how I can fix this?


Solution

  • Given that you have followed the ROS tutorial and have created an HTML file as shown in Ros Bridge tutorial then you have to run:

    • runcore
    • rosrun rospy_tutorials add_two_ints_server
    • roslaunch rosbridge_server rosbridge_websocket.launch

    Now that you have these up and running, you need to serve the html/javascript file (e.g. simple.html) and start the services etc. For example, you can serve the simple.html by using a SimpleHTTPServer, see below an example (e.g. simplehttpserver_test.py):

    #!/usr/bin/env python
    import SimpleHTTPServer
    import SocketServer
    
    class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):    
        def do_GET(self):    
            if self.path == '/':
                self.path = '/simple.html'            
            return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)        
    
    Handler = MyRequestHandler
    server = SocketServer.TCPServer(('127.0.0.1', 9089), Handler)    
    server.serve_forever() 
    

    Once you run the simplehttpserver_test.py you can open the browser on 127.0.0.1:9089 and you should be able to have it working.

    Note that SimpleHTTPServer serves files from the current directory and below, directly mapping the directory structure to HTTP requests, which means that the simple.html should be in the same (or below) directory as the simplehttpserver_test.py. Last, the port for the simplehttpserver_test.py should differ from the one used for the Rosbridge WebSocket server (e.g. default is 9090).