Advertisement
Whistik

Untitled

Mar 4th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. The problem I faced was, serving the client socket.io.js from a different location.
  2. You can avoid this issue by serving the client js file from the same server where you are trying to connect to.
  3. for example, my initial client code was this and it was throwing error
  4.  
  5. <script src="/socket.io/socket.io.js"></script>
  6. var socket = io.connect('http://mydomain.com/');
  7.  
  8. -----
  9. once I modified it to this, it worked alright.
  10. -----
  11.  
  12. <script src="http://mydomain.com/socket.io/socket.io.js"></script>
  13. var socket = io.connect('http://mydomain.com/');
  14.  
  15. -----
  16. And my server code is,
  17. -----
  18.  
  19. var express = require('express');
  20. var app = express();
  21. app.use(function(req, res, next) {
  22. res.header("Access-Control-Allow-Origin", "*");
  23. res.header("Access-Control-Allow-Headers", "X-Requested-With");
  24. res.header("Access-Control-Allow-Headers", "Content-Type");
  25. res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
  26. next();
  27. });
  28. var server = http.createServer(app);
  29. io = socketio.listen(server, {log:false, origins:'*:*'});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement