Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

node.js - Socket.io + NodeJS IONIC CORS issue

I hope you are doing well.

I am currently facing an issue with my IONIC application.

Setup :

  • IONIC 5 Application with ngx-socket-io module installed
  • NodeJS Back-End with express API and socket.io server listening on xxx.xxx.xxx.xxx:3000
  • socket.io and socket.io-client both in version 2.4.0

Here's the code I am using on IONIC side :

this._storage.get(`setting:loginToken`).then(setToken => {
      alert("Connecting to socket.io server...")
      this.socket.connect();
      alert("Should be connected...")
      this.socket.emit('authenticate', { token: setToken });
      alert("Auth token sent")
    }, error => console.log(error));

This is what i'm getting in my browser developer tools when I am running the application with ionic serve :

Blocking a Cross-Origin Request: The "Same Origin" policy does not allow consulting the remote resource located at http://XXX.XXX.XXX.XXX:3000/socket.io/?EIO=3&transport=polling&t=NTkQPqH. Reason: The CORS header "Access-Control-Allow-Origin" is missing.

I experiment the same problem using the APK build on an Android device.

But my other "non socket-io" requests on my express API are just performing fine from IONIC side.

Here is the back-end code I wrote :

const app = express();
app.use(cors());

var server = app.listen(config.port, () => {
  console.log('Server is listening on %d in %s mode.', config.port, app.get('env'));
});

var io = require('socket.io')(server)
const socketioJwt = require('socketio-jwt');

io.sockets
  .on('connection', socketioJwt.authorize({
    secret: 'stackoverflow',
    timeout: 15000
  }))
  .on('authenticated', (socket) => {

    socket.on("disconnect", () => {
    })

  });


Connecting with socket.io-client works just fine to simulate a client.

What can be wrong in this situation ?

Thank you for any help or advices you can provide.

question from:https://stackoverflow.com/questions/66052356/socket-io-nodejs-ionic-cors-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you need to add the CORS to the io instance:

For v2.x.x

require('socket.io')(server, {
  origins: [`http://localhost:${config.port}`]
})

https://socket.io/docs/v2/handling-cors/

For v3.x.x:

require('socket.io')(server, {
  cors: {
    origin: `http://localhost:${config.port}`,
    methods: ['GET', 'POST']
  }
})

https://socket.io/docs/v3/handling-cors/

I'm not sure if you'll need CORS on the express app since you seem to be using sockets exclusively but that's up to you of course.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...