简体   繁体   English

使用 Heroku 部署 NodeJS

[英]NodeJS deploying with Heroku

I am trying to upload my NodeJS project on Heroku.我正在尝试在 Heroku 上上传我的 NodeJS 项目。 The project is a multiplayer game, locally the code works for me and both players enter the same map.该项目是一个多人游戏,本地代码适用于我,两个玩家输入相同的 map。 But, in Heroku I don't get both players on the same map.但是,在 Heroku 中,我没有让两个玩家都在同一个 map 上。

I leave the NODEJS code我留下了 NODEJS 代码

const express = require("express")
const cors = require("cors")

const app = express()

app.use(express.static('public'))
app.use(cors())
app.use(express.json())

const jugadores = []

const PORT = process.env.PORT || 8080

class Jugador {
  constructor(id) {
    this.id = id
  }

  asignarMokepon(mokepon) {
    this.mokepon = mokepon
  }

  actualizarPosicion(x, y) {
    this.x = x
    this.y = y
  }

  asignarAtaques(ataques) {
    this.ataques = ataques
  }
}

class Mokepon {
  constructor(nombre) {
    this.nombre = nombre
  }
}

app.get("/unirse", (req, res) => {
  const id = `${Math.random()}`

  const jugador = new Jugador(id)

  jugadores.push(jugador)

  res.setHeader("Access-Control-Allow-Origin", "*")
  
  res.send(id)
})

app.post("/mokepon/:jugadorId", (req, res) => {
  const jugadorId = req.params.jugadorId || ""
  const nombre = req.body.mokepon || ""
  const mokepon = new Mokepon(nombre)
  
  const jugadorIndex = jugadores.findIndex((jugador) => jugadorId === jugador.id)

  if (jugadorIndex >= 0) {
    jugadores[jugadorIndex].asignarMokepon(mokepon)
  }
  
  console.log(jugadores)
  console.log(jugadorId)
  res.end()
})

app.listen(PORT, () => {
  console.log("Servidor funcionando", PORT)
})

I leave a small part of the code here because it is not possible to publish so much code.我在这里留下一小部分代码,因为不可能发布这么多代码。 But I leave a link to the repository on GitHub但是我在 GitHub 上留下了一个存储库的链接

Link of the page hosted on Heroku: https://proyecto-mokepon.herokuapp.com/ Code link on GitHub: https://github.com/IamMatiasBazan/proyecto-mokepon Link of the page hosted on Heroku: https://proyecto-mokepon.herokuapp.com/ Code link on GitHub: https://github.com/IamMatiasBazan/proyecto-mokepon

Locally it generates the random number for each player enter image description here在本地为每个玩家生成随机数在此处输入图像描述

Deployed in Heroku I see this: enter image description here部署在 Heroku 我看到了这个:在此处输入图像描述

The requests you are sending is pointed to localhost in your js file (multiple places, but this is one).您发送的请求在您的 js 文件中指向 localhost (多个地方,但这是一个)。 Here you should consider changing it to be the heroku domain or just /mokepon/${jugadorId}/ataques (or something else).在这里,您应该考虑将其更改为 heroku 域或只是/mokepon/${jugadorId}/ataques (或其他)。

It also looks like the app is sending a new request every 50ms, even if nothing happens.看起来应用程序每 50 毫秒发送一个新请求,即使什么也没发生。 In this case I would then suggest you to look into socket.io to prevent the application to send useless request everytime.在这种情况下,我建议您查看socket.io以防止应用程序每次都发送无用的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM