与另一台具有React Chat-App和socket.io的PC聊天
我遵循了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您研究在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