I have installed socket.io on my localhost and now I would view a client/server communication. I have started the server with node 'server.js'. I get this error
ERR_NAME_NOT_RESOLVED
with this simple lines of code:
SERVER:
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
console.log('emit...');
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
socket.on('pong', function (data) {
console.log(data.message);
});
});
console.log('listening on port 8080');
CLIENT:
<script type="text/javascript" src="js/socket.io.js"></script>
<script>
var socket = io.connect('http:\\192.168.1.129:8080');
socket.on('connect', function() {
socket.emit('pong',{message:"ciaooo"});
socket.on('ping', function (data) {
alert(data);
});
});
</script>
Problem: the socket is not able to connectYou are trying to reach a plain WebSocket server. The server is not reachable. The client is not compatible with the version of the server. The server does not send the necessary CORS headers.
Navigate into the client folder via your terminal and create a new React. js project. Install Socket.io client API and React Router. React Router is a JavaScript library that enables us to navigate between pages in a React application.
Try to replace
io.connect('http:\\192.168.1.129:8080')
with
io.connect('http://192.168.1.129:8080')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With