This is my first.py
file.
from flask import Flask
from flask_socketio import SocketIO,send
app=Flask(__name__)
app.config['SECRET_KEY']='myscret'
socketio=SocketIO(app)
@socketio.on('message')
def handlemessage(msg):
print('Message:'+msg)
send(msg,broadcast=True)
if __name__=='__main__':
socketio.run(app,debug=True)
This is index.html
file.
<html>
<head>
<title>Chat room</title>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" >
$(document).ready(function()
{
var socket=io.connect('http://127.0.0.1:5000');
socket.on('connect',function()
{
socket.send('User has connected');
});
});
</script>
<ul id="messages"></ul>
<input type="text" id="myMessage">
<button id="sendbtn">Send</button>
</body>
</html>
But when I run first.py
file and open index.html
in browser following error occurs.
Access to XMLHttpRequest at 'http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=N9J4bHD' from
origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on
the requested resource. index.html:1
GET 127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=N9J4bHD:1 Failed to load resource:
net::ERR_FAILED socket.io.min.js:2
Access to XMLHttpRequest at 'http://127.0.0.1:5000/socket.io/?
EIO=3&transport=polling&t=N9J4bQM' from origin 'null' has been blocked by CORS policy: No 'Access-
Control-Allow-Origin' header is present on the requested resource.
127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=N9J4bQM:1 Failed to load resource:
net::ERR_FAILED
Access to XMLHttpRequest at 'http://127.0.0.1:5000/socket.io/?
EIO=3&transport=polling&t=N9J4c8L' from origin 'null' has been blocked by CORS policy: No 'Access-
Control-Allow-Origin' header is present on the requested resource.
What should I do to solve this error? Please help.