Javascript Vue Socket.io:客户端不是';无法从服务器接收消息

Javascript Vue Socket.io:客户端不是';无法从服务器接收消息,javascript,node.js,vue.js,websocket,socket.io,Javascript,Node.js,Vue.js,Websocket,Socket.io,我正在使用Vue和Socket.io构建一个多人游戏。我能够将数据从客户机发送到服务器,但反过来就不行了,我也不知道为什么 以下是我的一些代码: app.js var app = express(); app.use(cors()); var server = require('http').createServer(app); var io = require('socket.io')(server); server.listen(4000); // socket io io.on('co

我正在使用Vue和Socket.io构建一个多人游戏。我能够将数据从客户机发送到服务器,但反过来就不行了,我也不知道为什么

以下是我的一些代码:

app.js

var app = express();
app.use(cors());
var server = require('http').createServer(app);
var io = require('socket.io')(server);

server.listen(4000);

// socket io
io.on('connection', function (socket) {
  console.log('User connected ' + socket.id);
  socket.on('disconnect', function(data) {
    console.log('User disconnected ' + this.id);
  });
  socket.on('joined-game', function(data) {
    console.log('player joined game' + this.id);
    io.emit('start-game', { game: data });
  });
});
JoinGame.vue中

import axios from 'axios'; 
import * as io from 'socket.io-client';
export default {
  data() {
    return { games: "", invite: "", nickname: "", socket: io('http://localhost:4000') }
  },
  methods: {
    joinGame() {
      axios.get('http://localhost:5000/api/game/' + this.invite)
        .then(response => {
          this.games = response.data;
          this.socket.emit('joined-game', { game: this.invite, nickname: this.nickname });
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
}
import axios from 'axios'; 
import * as io from 'socket.io-client';
export default {
  data() {
    return { invite: "", addedGame: false, waiting: true, socket: io('http://localhost:4000')  }
  },
  methods: {
    mounted() {
      this.socket.on('start-game', function (data) {
        console.log('player 2 joined game');
        if(data.game === this.invite) {
          this.waiting = false;
        }
      }.bind(this));
    }
  }
}
AddPlayer.vue中

import axios from 'axios'; 
import * as io from 'socket.io-client';
export default {
  data() {
    return { games: "", invite: "", nickname: "", socket: io('http://localhost:4000') }
  },
  methods: {
    joinGame() {
      axios.get('http://localhost:5000/api/game/' + this.invite)
        .then(response => {
          this.games = response.data;
          this.socket.emit('joined-game', { game: this.invite, nickname: this.nickname });
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
}
import axios from 'axios'; 
import * as io from 'socket.io-client';
export default {
  data() {
    return { invite: "", addedGame: false, waiting: true, socket: io('http://localhost:4000')  }
  },
  methods: {
    mounted() {
      this.socket.on('start-game', function (data) {
        console.log('player 2 joined game');
        if(data.game === this.invite) {
          this.waiting = false;
        }
      }.bind(this));
    }
  }
}

我可以将“已加入的游戏”从客户端发送到服务器,但看起来“开始游戏”没有从服务器发送到客户端

您已经在方法中装入了函数。试着把它放在外面


您已经在方法中装入了函数。试着把它放在外面


以防任何人出现问题,客户无法接收到发射信号, 我的问题是:

我在插座中添加了插座,如下所示:

    connect() {
      this.isConnected = true;
      console.log("socket hit");
    }
  },
由于某种原因,这不起作用(参考了一些教程,发现了这一点)

在“mounted()”中,我什么都没有。我现在补充说:

    this.socket.on('connect', () => {
      console.log("emit received from server");
    });
  },

这工作正常,我可以看到emit。

以防任何人出现问题,客户机无法接收emit, 我的问题是:

我在插座中添加了插座,如下所示:

    connect() {
      this.isConnected = true;
      console.log("socket hit");
    }
  },
由于某种原因,这不起作用(参考了一些教程,发现了这一点)

在“mounted()”中,我什么都没有。我现在补充说:

    this.socket.on('connect', () => {
      console.log("emit received from server");
    });
  },

这工作正常,我可以看到发射。

谢谢!!真不敢相信我错过了啊哈现在一切都很好谢谢!!我真不敢相信我错过了啊哈现在一切都很好