1

I'm trying to get websockets to work between two machines. One pc and one raspberry pi to be exact. On the PC I'm using socket.io as a client to connect to the server on the raspberry pi. With the following code I iniated the connection and try to send predefined data.

var socket = io.connect(ip + ':8080');
socket.send('volumes', { data: data });

On the raspberry pi, the websocket server looks like this:

from tornado import web, ioloop
from sockjs.tornado import SockJSRouter, SockJSConnection

class EchoConnection(SockJSConnection):

    def on_message(self, msg):
        self.send(msg)


    def check_origin(self, origin):
        return True

if __name__ == '__main__':
    EchoRouter = SockJSRouter(EchoConnection, '/echo')

    app = web.Application(EchoRouter.urls)
    app.listen(8080)
    ioloop.IOLoop.instance().start()

But the connection is never established. And I don't know why. In the server log I get:

WARNING:tornado.access:404 GET /socket.io/1/?t=1412865634790 (192.168.0.16) 9.01ms

And in the Inspector on the pc there is this error message:

XMLHttpRequest cannot load http://192.168.0.10:8080/socket.io/1/?t=1412865634790. Origin sp://793b6d4588ead99e1780e35b71d24d1b285328f8.hue is not allowed by Access-Control-Allow-Origin. 

I am out of ideas and don't know what to do. Can you help me? Thank you!

1 Answer 1

2

Well, the solution for your problem has to do with the internal design of the sockjs-tornado library more than with the socket.io library.

Basically, your problem has to do with cross origin request i.e. the html that is generating the request to the websocket server is not at the same origin as the websocket server. I can see from your code that you already identified the problem ( and you tried to solve it by redefining the method "check_origin") but you didn´t find the proper way to do it, basically because within this library is not the SockJSConnection class the one that extends tornado WebSocketHandler and so redefining its "check_origin" is useless. If you dig a little bit into the code, you will see that there exists one class defined, namely SockJSWebSocketHandler that has a redefinition of such method itself, which relies on the tornado implementation if it returns true, but that also allows you to avoid that check using a setting parameter :

class SockJSWebSocketHandler(websocket.WebSocketHandler):
    def check_origin(self, origin):
        ***
        allow_origin = self.server.settings.get("websocket_allow_origin", "*")
            if allow_origin == "*":
                return True

So, to summarize, you just need to include the setting "websocket_allow_origin"="*" in the server settings and everything should work properly =D

if __name__ == '__main__':
    EchoRouter = SockJSRouter(EchoConnection, '/echo', user_settings={"websocket_allow_origin":"*"})
5
  • I just tried your suggestion but I keep getting the same 404 message as above. I've got a nginx webserver going on the machine as well. could tornado interfere with that?
    – kirijanker
    Oct 9, 2014 at 17:07
  • It shouldnt ... Can it be something about the protocol used in the request? What is the value of "ip" variable? Oct 9, 2014 at 17:12
  • Awwww yeah! Thank you! I got it working. I just switched from socket.io to sockjs for the client and just got my first text message through. Should I post the code herre, if somebody has the same problem?
    – kirijanker
    Oct 9, 2014 at 17:47
  • Yes you should, for future references =D Oct 9, 2014 at 18:26
  • 1
    @kirijanker you should have posted that working code it would have been helpful.
    – jcoffland
    Jan 7, 2016 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.