gpt4 book ai didi

Golang 超时或提前返回

转载 作者:IT王子 更新时间:2023-10-29 01:53:45 26 4
gpt4 key购买 nike

我正在尝试使用 websockets 作为学习项目在 Golang 中构建一个实时多人游戏服务器,但我很难理解我应该如何实现这个概念:

游戏室中的所有用户都被赋予一个持续时间,MAX_TIMEOUT 以响应提供的提示。响应通过 websocket 连接提交。如果所有用户都在 MAX_TIMEOUT 之前做出响应,则应该使用这些响应来执行 Action A。如果在提交所有提示之前 MAX_TIMEOUT 已过去,则应该使用可用的响应执行 Action B

来自 Node.JS,我可以看到自己用 Promises 实现它,但在 Golang 中,我很迷茫。

有什么建议吗?

最佳答案

结帐 gorilla/websocket's chat example .如上所述,它在 client.go 中使用代码和 channel 。

服务器的提示处理可以是它自己的与读/写泵例程并行运行的例程。

go func() {
for {
for _, player := range players {
player.Answered = false
player.Send("prompt")
}

time.Sleep(time.Second * 10)

for _, player := range players {
if player.Answered {
player.Send("You answered")
} else {
player.Send("Too slow")
}
}
}
}()

// the reading routine sets `Answered` true when message received

关于Golang 超时或提前返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com