gpt4 book ai didi

c# - 光子 bolt |创建后将房间设为私有(private)

转载 作者:行者123 更新时间:2023-12-04 17:26:06 32 4
gpt4 key购买 nike

我知道如何私下玩游戏。

BoltLauncher.StartSinglePlayer

但是这次不是离线玩游戏,我想知道的是如何将房间设为私有(private),这样就没有更多的玩家可以加入。

我将 IsOpen 设置为 false 并在玩家需要时将其设为私有(private),但其他客户端可以加入房间。

有谁知道如何在服务器所有者想要时设置私有(private)?

最佳答案

将接受模式设置为手动。

image of accept mode being highlighted. source: link below

覆盖 ConnectRequest 以检查您必须跟踪的任何变量,如果 session 应该是私有(private)的,并调用 BoltNetwork.Refuse 如果它应该是私有(private)的,或者如果连接应该被允许,BoltNetwork.Accept:

private bool serverShouldBePrivateNow; 

// ...

public override void ConnectRequest(UdpEndPoint endpoint, Bolt.IProtocolToken token) {
bool connectionShouldBeRejected = false;

connectionShouldBeRejected = connectionShouldBeRejected || serverShouldBePrivateNow;

// any other appropriate checks go here
// connectionShouldBeRejected = connectionShouldBeRejected || ...;
// connectionShouldBeRejected = connectionShouldBeRejected || ...;
// connectionShouldBeRejected = connectionShouldBeRejected || ...;

if (connectionShouldBeRejected)
{
BoltNetwork.Refuse(endpoint);
}
else
{
BoltNetwork.Accept(endpoint);
}
}

如果您需要向客户端返回数据(例如连接被拒绝的确切原因的解释),您可以使用 BoltNetwork.Refuse(UdpEndPoint endpoint, IProtocolToken refuseToken); 以 token 响应;:

  1. 定义一个IProtocolToken:
using UdpKit;

public class RequestToken : Bolt.IProtocolToken
{
public enum RefuseReason {privateReason1, privateReason2}

// other reasons go here
public RefuseReason reason { get; set; }

public void Read(UdpPacket packet)
{
this.reason = (RefuseReason)packet.ReadInt();
}

public void Write(UdpPacket packet)
{
packet.WriteInt((int)this.reason);
}
}
  1. ConnectRequest 中的服务器代码中使用它:
public override void ConnectRequest(UdpEndPoint endpoint, Bolt.IProtocolToken token) {
bool connectionShouldBeRejected = false;

connectionShouldBeRejected = connectionShouldBeRejected || serverShouldBePrivateNow;

if (connectionShouldBeRejected)
{
RequestToken token = new RequestToken();
if (serverShouldBePrivateNow)
{
token.reason = RequestToken.RefuseReason.privateReason1;
}
else
{
token.reason = RequestToken.RefuseReason.privateReason2;
}

BoltNetwork.Refuse(endpoint, token);
}
else
{
BoltNetwork.Accept(endpoint);
}
}
  1. void ConnectRefused(UdpEndPoint endpoint, Bolt.IProtocolToken token) 的客户端代码中检查:
public override void ConnectRefused(UdpEndpoint endpoint, Bolt.IProtocolToken token)
{
Debug.Assert(token is RequestToken);
RequestToken reqTok = (RequestToken)token;

switch (token.reason)
{
case RequestToken.RefuseReason.privateReason1:
// handle private Reason 1
break;
case RequestToken.RefuseReason.privateReason2:
// handle private Reason 2
break;
default:
// handle error in packet
Debug.Assert(false, "Undefined value for enum");
}
}

参见 Accepting and Refusing Connections on the Bolt wiki了解更多信息

关于c# - 光子 bolt |创建后将房间设为私有(private),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63325549/

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