1

There is a php-file on apache:

<script src="/../node_modules/socket.io/client-dist/socket.io.js"></script>

let socket = io.connect( 'http://localhost:3000' );

socket.on('update', function updateData(sounds) {
    console.log(sounds);
});

and node js in same directory

let http = require('http');
let express = require('express');
let path = require('path');
let socketIO = require('socket.io');
let app = express();
let server = http.Server(app);
let io = socketIO(server);

app.set('port', 3000);

server.listen(3000, function() {
   console.log('listening on *:3000');
});

io.on('connection', function(socket) {
   console.log('A user connected');
  
   socket.on('disconnect', function() {
      console.log('A user disconnected');
   });
   
});

browser throws an error: GET http://192.168.100.31/socket.io/?EIO=4&transport=polling&t=NxOsf3z 404 (Not Found)

if replace let socket = io.connect( 'http://localhost:3000' ) on let socket = io.connect( 'http://192.168.100.31:3000' ); browser will give another error: Access to XMLHttpRequest at 'http://192.168.100.31:3000/socket.io/?EIO=4&transport=polling&t=NxOt0X0' from origin 'http://192.168.100.31' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

1 Answer 1

2

Just had to replace

let socket = io.connect( 'http://localhost:3000' ); 

with

let socket = io.connect( 'http://192.168.100.31:3000/' );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.