从内部回调中断父函数的执行(Break execution of parent function from inner callback)

编程入门 行业动态 更新时间:2024-10-05 11:15:00
从内部回调中断父函数的执行(Break execution of parent function from inner callback)

我正在为Unity3D创建一个nodejs服务器,使用socketio进行网络连接。 我有一个作为比赛室的功能,我想在玩家离开游戏时离开该功能。 每次玩家创建一个新房间时,它应该进入该功能并重新开始。 是否有可能从子回调中断执行父函数?

exports.initGame = function (userdata) { //do prematch stuff with userdata and execute game logic on the callbaks socket.on("something", function (someNetdata)({ // how do i stop parent function from here? }); // more socket.on callbacks }

编辑:忘了提到函数正在导出。 不知道是否有任何改变。

编辑2:鉴于jfriend00回答中讨论的建议,我将对问题进行更广泛的解释。

目前的流程如下:

用户连接,我在Io“连接”回调上定义了一些功能。 相关的一个是startMatchMaker(Io, socket, data) 。 matchMaker为玩家创建一个空间(如果有一个可用,则加入一个)并继续执行之前提到的gameInit(io, socket, room, data) 。

gameInit(...)函数以房间为关键字存储全局数组的相关数据,例如, Players[room] = {/* player object */} 。 匹配是1对1,所以当一个玩家放弃游戏时,我从阵列中删除条目,引用该房间: delete Players[room] 。 当玩家移动时,我在全局数组中更新其对象上的玩家位置。 这是在回调中完成的,在gameInit(...)

socket.on("move", function(data){ //do movement logic and update player position, if it is valid. Omitting the rest of the code. Players[room].position = data.position; });

现在让我们想象两个玩家,player1和player2在room1中玩。 Player2退出服务器放弃游戏。 Player1仍然连接,但必须离开当前的游戏。 删除所有变量(如前所述),player1加入room2中的新游戏。 当player1试图移动时,将调用2个回调:在第一个initGame中添加的room = "room1" ,另一个在执行第二个匹配时添加: room = "room2" 。 抛出异常是因为在key = room1玩家中没有条目。

鉴于此,解决方案是从上一个匹配中删除侦听器 。

I'm creating a nodejs server for Unity3D, using socketio for the networking. I have a function that serves as match room and I want to leave that function when the player leaves the game. Each time the player creates a new room it should enter the function and start over. Is it possible to break execution of parent function from a child callback?

exports.initGame = function (userdata) { //do prematch stuff with userdata and execute game logic on the callbaks socket.on("something", function (someNetdata)({ // how do i stop parent function from here? }); // more socket.on callbacks }

EDIT: Forgot to mention that the function is being exported. Don't know if that change anything.

EDIT2: Given the suggestions discussed in jfriend00 answer, I'll give a more extended explanation of the problem.

The current flow is as it follows:

user connects and I define some functions on the Io "connected" callback. Relevant one is startMatchMaker(Io, socket, data). The matchMaker creates a room for the player (or joins one if there is one available) and proceed to executing the gameInit(io, socket, room, data) mentioned before.

The gameInit(...) function stores relevant data on global arrays with the room as key, for example, Players[room] = {/* player object */}. The match is 1 vs 1, so when one player abandons the game, I remove the entries from the arrays, referring that room: delete Players[room]. When the player moves, I update the player position on their object inside the global array. this is done on a callback, added inside the gameInit(...)

socket.on("move", function(data){ //do movement logic and update player position, if it is valid. Omitting the rest of the code. Players[room].position = data.position; });

Now lets imagine two players, player1 and player2 playing in room1. Player2 abandon the game by exiting the server. Player1 is still connected, but must leave that current game. All the variables are deleted (as mentioned before) and player1 joins a new game in room2. When player1 tries to move, 2 callbacks will be called: the one that was added in the first initGame with room = "room1" and another added upon execution of the second match: room = "room2". A exception is thrown because there is no entry in Players with key = room1.

Given this the solution is to remove the listeners from the previous match.

最满意答案

你的“父母”功能的概念有点混乱。 Javascript中没有“父函数”。

看看上面的例子,看起来像socket.on回调函数,你想做一些影响该函数之外的代码的东西。 这里要认识到的重要一点是它是一个回调函数 - 当套接字收到一个标识为something的消息时,它会调用回调函数。 没有涉及父母/子女关系。

所以现在更清楚一点,让我们考虑一下你的问题。 你没有在回调之外准确解释你想要做什么,所以不清楚你想要停止什么功能。 但这并不是非常重要 - 基本上,你要做的就是在回调内部发生某些事情时在回调之外发出信号代码。 有很多方法可以做到这一点,但一种基本技术是使用变量作为信号。

值得注意的是 node.js应用程序(通常)是事件驱动的。 这意味着你的代码几乎总是在回调中响应正在发生的事情 - 没有“主循环”等待事情发生。

所以你的代码看起来像这样:

exports.initGame = function (userdata) { // do prematch stuff with userdata and execute game logic on // the callbacks // set up a variable to signal when something important happens var importantSignal = null; socket.on("something", function (someNetdata)({ // something important has happened! let the rest of the application know about it importantSignal = someNetdata.message; // do some other stuff related to the callback }); // another socket event socket.on("another", function (someNetData) { // has something important happened? if (importantSignal) { console.log("I see that 'something' has already happened!"); console.log("Message = " + importantSignal); } }); }

这是在回调之间传递的一种简单类型的消息 - 请注意第二个回调可以访问在第一个回调中设置的某个状态。 但是,第一个回调中的代码“控制”第二个回调是没有意义的,除非第二个回调根据第一个回调中设置的状态做出决策。

Your notion of "parent" function is a little confused. There are no "parent functions" in Javascript.

Looking at your example above, it seems like inside the socket.on callback function you want to do something that affects code outside that function. The important thing to recognize here is that it's a callback function - when the socket receives a message identified as something, it calls the callback function. There's no parent/child relationship involved.

So now that's a little clearer, let's consider your problem. You haven't explained exactly what you want to do outside the callback, so it's not clear what function you want stopped. But that's not terribly important - basically, what you're trying to do is signal code outside the callback when something inside the callback happens. There are a number of ways to do this, but one basic technique is to use a variable as a signal.

It is important to note that node.js applications are (usually) event-driven. This means that your code will almost always be in callbacks responding to stuff that is happening - there is no "main loop" that is waiting for stuff to happen.

So your code might look something like this:

exports.initGame = function (userdata) { // do prematch stuff with userdata and execute game logic on // the callbacks // set up a variable to signal when something important happens var importantSignal = null; socket.on("something", function (someNetdata)({ // something important has happened! let the rest of the application know about it importantSignal = someNetdata.message; // do some other stuff related to the callback }); // another socket event socket.on("another", function (someNetData) { // has something important happened? if (importantSignal) { console.log("I see that 'something' has already happened!"); console.log("Message = " + importantSignal); } }); }

This is a simple type of message passing between callbacks - notice that the second callback has access to some state that was set in the first callback. But there's no sense in which the code in the first callback "controls" the second callback, except insofar as the second callback makes decisions based on state set in the first callback.

更多推荐

本文发布于:2023-08-07 12:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   函数   Break   execution   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!