[英]Browser connecting with multiple sockets using JS socket.io
I'm using node.js with a React front end. 我正在使用带有React前端的node.js。 I'm building a GPS based MMO type of game.
我正在构建基于GPS的MMO类型的游戏。 I recently decided to drop most HTTP requests and go with sockets, so I can emit data whenever I want and the front end only has to worry about what to do with it when it receives it.
我最近决定放弃大多数HTTP请求并使用套接字,因此我可以随时发送数据,并且前端只需要担心在接收到它时该如何处理。
I've built sites with sockets before, but never ran into this issue. 我以前用套接字构建过站点,但从未遇到过此问题。
Basically, every time my browser opens a socket connection with node, it opens 2-3 connections at once(?). 基本上,每次我的浏览器打开与节点的套接字连接时,它都会一次打开2-3个连接(?)。 When it disconnects, I get the console.log stating that 3 socket connectionss have been closed.
当它断开连接时,我得到console.log指出3个套接字连接已关闭。 It'll look like this:
User disconnected User disconnected A user has connected to the system with id: nUMbkgX6gleq-JZQAAAD A user has connected to the system with id: CzFtR2K5NJ1SoiHLAAAE A user has connected to the system with id: tgGYhpXuOONmL0rMAAAF
它看起来像这样:
User disconnected User disconnected A user has connected to the system with id: nUMbkgX6gleq-JZQAAAD A user has connected to the system with id: CzFtR2K5NJ1SoiHLAAAE A user has connected to the system with id: tgGYhpXuOONmL0rMAAAF
For now, it wouldn't be an issue, however I'm only getting the FIRST 'inventory' emit to work. 目前,这不是问题,但是我只能使第一个“库存”排放起作用。 Later when I call the function to emit inventory again, the browser doesn't seem to get it.
稍后,当我调用该函数再次发出清单时,浏览器似乎没有收到它。 But the console logs in the node function will trigger correctly.
但是控制台登录节点功能将正确触发。 I have a feeling that this has something to do with multiple sockets opening.
我感觉这与多个插座打开有关。
Here is the React Event: 这是React事件:
this.socket.on("inventory", charData => { console.log('heres the data', charData) props.setCharStats(charData[0]) })
Here's the node emitter:
const getCharInventory = (charId, userId) => { dbInstance .getCharInventory(charId) .then(response => { console.log( // this console.log happens just fine "emmited inventory, userId is: ", userId, " with this response: ", response) socket.emit("inventory", response) }) .catch(err => console.error(err)) }
I'm such a dork. 我真傻 I was starting multiple connections within multiple connections.
我正在多个连接中启动多个连接。 In more than one component, I had this...
在一个以上的组件中,我拥有了……
this.socket = socketIOClient(`${process.env.REACT_APP_proxy}`)
Sooo..... yeah. 太好了。 Just an FYI to others, it's better to connect in 1 file and import it into others.
只是供他人参考,最好将一个文件连接起来并将其导入到其他人中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.