S
S
Sergey Beloventsev2016-09-01 20:56:37
Node.js
Sergey Beloventsev, 2016-09-01 20:56:37

Why does this error occur with sockets?

here is the server code

var http = require('http');
    var fs = require('fs');
    var server = http.createServer(app);
    server.listen(config.get('port'), function(){
      log.info('Express server listening on port ' + config.get('port'));
    });
    
    var io = require('socket.io').listen(server);
    function handler (req, res) {
      fs.readFile(__dirname + '/index.html',
          function (err, data) {
            if (err) {
              res.writeHead(500);
              return res.end('Error loading index.html');
            }
    
            res.writeHead(200);
            res.end(data);
          });
    }
    
    io.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });

here is the client
<script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io('http://localhost');
        socket.on('news',function (data) {
            console.log(data);
            socket.emit('my other event',{my:'data'});
        });
    </script>

server connects here
/usr/bin/nodejs /var/www/html/chatnode/app.js
    info: [chatnode/app.js] Express server listening on port 3000

and here is an error in the browser on the client page
GET http://localhost/socket.io/?EIO=3&transport=polling&t=LRcrDTD
    	
    404 Not Found 0ms

here are the headlines
Connection	: Keep-Alive
    Content-Length	: 283
    Content-Type	: text/html; charset=iso-8859-1
    Date	: Thu, 01 Sep 2016 17:38:43 GMT
    Keep-Alive	: timeout=5, max=98
    Server	Apache/2.4.18 (Ubuntu)
    

    Accept	: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding	: gzip, deflate
    Accept-Language	: en-US,en;q=0.5
    Cookie	: sid=s%3AO5ULQ9g2P90WX7Ypsh5e9ELy.%2FgllbE05X1GwmsSw%2FcId3VbnFPYFgnhZ%2BkJs6i54Jok
    DNT	: 1
    Host	: localhost
    Origin	: http://localhost:3000
    Referer	: http://localhost:3000/chat
    User-Agent	: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0

here is the query
EIO	        3
    t           LRcrDTD
    transport	polling

and here's the answer
null
, what's wrong? Why error with sockets?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Volintsev, 2016-09-04
@Sergalas

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:3000'); // <-- хост:порт
socket.on('news',function (data) {
    console.log(data);
    socket.emit('my other event',{my:'data'});
});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question