反应和套接字,客户端不发射

时间:2021-02-09 16:14:53

标签: reactjs socket.io react-router

我正在尝试从客户端向服务器端发送数据,但目前它只是没有发送。我以前使用过套接字,但我对做出反应还很陌生,所以我不确定我做错了什么。

所以首先我在单个文件中连接套接字,然后将其导入到任何需要使用它的文件中。

socket.jsx

import socketIOClient from "socket.io-client"
const ENDPOINT = "http://localhost:4001"
const socket = socketIOClient(ENDPOINT)

export default socket;

这是客户端的代码

modal.jsx

createLobby = (e) => {
  console.log(this.state)
  if(this.state.username === null || this.state.username === undefined) {return}
  if(this.state.maxplayers === null || this.state.maxplayers === undefined) {return}
  if(this.state.lobbyPrivacy === 'private'){
    if(this.state.password === null || this.state.password === undefined){return}
  }
  console.log('you got ere')
  socket.emit('/createLobby', {
    lobby: this.state,
    clientid: cookies.get('id')
  })
}

服务器端代码监听:

app.js

io.on("connection", (socket) => {

  console.log(chalk.hex('#eb5f9e')(`Socket ${socket.id} connected.`))
  io.on('/createLobby', (req, res) => {
    console.log('hallo')
  })
...

0 个答案:

没有答案