Почему возникает такая ошибка с сокетами?

вот собствено код сервера

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);
      });
    });


вот клиента

<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>


сервер подключается вот

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

а вот такая ошибка в браузере на странице клиента

GET http://localhost/socket.io/?EIO=3&transport=polling&t=LRcrDTD
    	
    404 Not Found 0ms


вот заголовки

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


вот запрос

EIO	        3
    t           LRcrDTD
    transport	polling


и вот ответ

null

что не так то ? Почему ошибка с сокетами?
  • Вопрос задан
  • 3473 просмотра
Решения вопроса 1
copist
@copist
Empower people to give
<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>
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы