与另一台具有React Chat-App和socket.io的PC聊天

发布于 2025-02-04 22:47:27 字数 878 浏览 2 评论 0原文

我遵循了YouTube教程,以与React JS和Socket.io制作聊天应用程序。它是完整的并且可以完美地工作,但是只有在PC上,该项目才能运行。我需要的是与该项目正在开发的另一台PC聊天的应用程序,它可能在同一网络上,用于初学者。前端是分开的,由React JS制成。
我尝试将虚拟IP添加到指定它将运行的端口的位置,但不起作用。
有什么想法吗?我在这里想念什么?
这是server.js的代码

const port = 5000 || '0.0.0.0' <--- Ignore the dummy IP kindly, didn't work, thought it was worth a try
const io = require('socket.io')(port)

io.on('connection', socket => {
  const id = socket.handshake.query.id
  socket.join(id)
  console.log("Listening at " + port)

  socket.on('send-message', ({ recipients, text }) => {
    recipients.forEach(recipient => {
      const newRecipients = recipients.filter(r => r !== recipient)
      newRecipients.push(id)
      socket.broadcast.to(recipient).emit('receive-message', {
        recipients: newRecipients, sender: id, text
      })
    })
  })
})

I followed a youtube tutorial on making a chat app with react js and socket.io. It is complete and works perfectly, but only on the pc, the project is running on. What I need is the application to chat with another pc the project is working on, it could be on the same network, for starters. The front-end is separate and is made with react js.
I tried adding a dummy IP to where I specify the port it will run on, but didn't work.
Any ideas? What am I missing here?
Here's the code to the server.js which is in plain node js:

const port = 5000 || '0.0.0.0' <--- Ignore the dummy IP kindly, didn't work, thought it was worth a try
const io = require('socket.io')(port)

io.on('connection', socket => {
  const id = socket.handshake.query.id
  socket.join(id)
  console.log("Listening at " + port)

  socket.on('send-message', ({ recipients, text }) => {
    recipients.forEach(recipient => {
      const newRecipients = recipients.filter(r => r !== recipient)
      newRecipients.push(id)
      socket.broadcast.to(recipient).emit('receive-message', {
        recipients: newRecipients, sender: id, text
      })
    })
  })
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-02-11 22:47:27

我建议您研究在Heroku或Firebase之类的内容上部署聊天应用程序,这将为您提供第三方非局部主机链接,其他PC和用户可以使用该链接以访问您的聊天应用程序。
另外,您可以使用 https://github.com/localtunnel/localtunnel/localtunnel 访问您的本地主机

I'd recommend you look into deploying your chat app on something like Heroku or Firebase, which will provide you a 3rd-party non-localhost link that other PCs and users can go to in order to access your chat app.
Alternatively, you can use something like https://github.com/localtunnel/localtunnel to have other PCs access your localhost

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文