How to get Socket.IO client to connect to Python3 Websocket server

I am trying to get a Socket.IO client to connect to a Python Websocket server that I created with the aaugustin websockets library and asyncio. Using the example on the page, I created the following web socket server:

import asyncio
import websockets
from datetime import datetime

@asyncio.coroutine
def producer():
   return str(datetime.now())

@asyncio.coroutine
def handler(websocket, path):
    while True:
        message = yield from producer()
        if not websocket.open:
            break
        yield from websocket.send(message)
        yield from asyncio.sleep(3)

start_server = websockets.serve(handler, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

      

The view is opened using Flask and looks like this:

<!doctype html>
<head>
    <title>Websocket Client Test</title>
</head>
<body>
    <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script>
        console.log(io);
        var socket = io("ws://localhost:8765/");
        console.log(socket);
    </script>
</body>

      

Whenever Socket.IO tries to connect, it throws the following error:

XMLHttpRequest cannot load http://localhost:8765/socket.io/?EIO=3&transport=polling&t=1434254272412-0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5001' is therefore not allowed access. The response had HTTP status code 400.

      

Access-Control-Allow-Origin assumes that I am trying to make a request to a location with a different hostname, which I am not. So I don't know why it was throwing this error. I created a Python client script that connects to the server just fine, so I am a bit lost on what I am missing.

+3


source to share


1 answer


Access-Control-Allow-Origin assumes that I am trying to make a request to a location with a different hostname, which I am not.

A different port is considered a different source for many implementations. From https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy :



Two pages have the same origin if the protocol, port (if specified) and host are the same for both pages

+2


source







All Articles