socket.io GET /socket.io/?EIO=3&transport=polling&t=LV9VGzC"错误(404):"未找到"

时间:2016-10-15 18:21:10

标签: angularjs node.js socket.io

//backend code
var express = require("express");
var app = express();
var http = require('http');

var startServer=http.createServer(app);
var socketIO = require('socket.io').listen(startServer);
startServer.listen(8080, function () {
    console.log('server running on',8080)
});

socketIO.on('connection', function (socket) {
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>connected')
    var userSocketObject = {};
    userSocketObject.socket_id = socket.id;
    socket.emit('news', { hello: 'world' });
        socket.on('my other event', function (data) {
        console.log(data);
    });
});

//index.html code angularjs
var socket = io.connect('http://127.0.0.1:8080');
socket.on('news', function(data){
    console.log(data);
    socket.emit('my other event', {my: 'data'});
});

我尝试连接但我在浏览器中收到错误:

GET http://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=LV9VGzC" Error (404): "Not found"

2 个答案:

答案 0 :(得分:1)

我已经解决了问题,问题出在目录路径app.use(express.static(path.join(' public')))

答案 1 :(得分:0)

尝试将后端代码更改为

var socketio = require('socket.io')(startServer, {
  path: '/socket.io-client'
});

socketio.on('connection', function (socket) {
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

在客户端,最简单的方法是按照此回购中的说明进行操作:https://github.com/btford/angular-socket-io