Socket.IO basic example not working

后端 未结 1 1113
Happy的楠姐
Happy的楠姐 2020-12-21 11:43

I\'m 100% new to Socket.IO and have just installed it. I was trying to follow some examples, and can get the server side running but I can\'t seem to get the client side con

相关标签:
1条回答
  • 2020-12-21 12:22

    In same origin policy, localhost:8090 and localhost are not the same origin (different port), so localhost/index.html cannot connect to socket.io on localhost:8090.

    One option is to make index.html on localhost:8090 (see code below). Then you just put "index.html" into the same directory of your server script, start the server and type localhost:8090 in your browser.

    var fs = require('fs');
    
    server = http.createServer(function(req, res){
        fs.readFile(__dirname + '/index.html', 'utf-8', function(err, data) { // read index.html in local file system
            if (err) {
                res.writeHead(404, {'Content-Type': 'text/html'});
                res.end('<h1>Page Not Found</h1>');
            } else { 
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.end(data);
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题