I have a socket.io server running and I cannot connect to it form my local html file on my mac.
Error:
Failed to load http://file/socket.io/?EIO=3&transport=polling&t=M6tFqlm: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'null' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Server:
var app = require('express')();
var server = app.listen(8080);
var cors = require('cors');
app.options('*', cors());
var io = require('socket.io').listen(server);
....
// think this is redudant
server.listen(8080);
Local HTML file (no local server running it)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
</head>
<body>
<script src="../socket.io-client/dist/socket.io.js"></script>
<script>
// Create SocketIO instance, connect
// var socket = new io.Socket();
var socket = io();
//var socket = io('https://example.com:8080', { transport : ['websocket'] });
socket.connect('https://example.com:8080');
// Add a connect listener
socket.on('connect',function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
};
</script>
<div id="date"></div>
<textarea id="text"></textarea>
</body>
</html>
<script src="../socket.io-client/dist/socket.io.js"></script>
to<script src="/socket.io/socket.io.js"></script>
– CodeLoverapp.options('*', cors());
toapp.use(cors());
? – CodeLover