Exemplo n.º 1
0
        public void StartScene(int sceneId)
        {
            currentSceneId = sceneId;
            currentServer  = new BaseServer();
            currentServer.Connect(ServerConst.ServerAddress, ServerConst.TcpRemotePort, ServerConst.UdpRemotePort, ServerConst.UdpLocalPort, true);
            Int32Data sceneIdData = new Int32Data();

            sceneIdData.Value = sceneId;
            currentServer.Request(HandlerConst.RequestId.RequsetSceneHandler, -1, sceneIdData, SendType.TCP, (reqSceneRes) =>
            {
                if (reqSceneRes.GetValue <BoolData>().Value == true)
                {
                    Debug.logger.Log("请求场景成功");
                    currentServer.Regist(new List <int> {
                        currentSceneId
                    }, new List <int>(), (pkg) =>
                    {
                        Debug.logger.Log("频道注册成功");
                        EventDispatcher.AddEventListener <NetBehavior>(NetSceneEvent.NetBehaviorAddToScene, AddNetBehavior);
                        EventDispatcher.AddEventListener <NetBehavior>(NetSceneEvent.NetBehaviorRemoveFromScene, RemoveNetBehavior);
                        sceneConnected = true;
                    });
                }
                else
                {
                    Debug.logger.LogError("NetPost", "请求场景失败");
                }
            });
        }
Exemplo n.º 2
0
 /// <summary>
 /// 处理包行为,分配给相应的Handler
 /// </summary>
 /// <param name="package"></param>
 /// <param name="act"></param>
 public static void HandlePackage(BaseServer server, Package package, Action act = null)
 {
     //触发响应事件(针对有响应的请求)
     HandlerConst.RequestId id = EnumEx.GetValue <HandlerConst.RequestId>(package.eventId);
     if (HandlerConst.handlerDict.ContainsKey(id))
     {
         HandlerConst.handlerDict[id].ownerServer = server;
         HandlerConst.handlerDict[id].Handle(package, act);
     }
     else
     {
         Debug.logger.Log("不存在id:" + id.ToString());
     }
     EventDispatcher.TriggerEvent <Package>(ServerEvent.GetResponseEvent(package.requestId), package, server);
     if (act != null)
     {
         act();
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 开启场景
        /// </summary>
        /// <param name="sceneId"></param>
        public void StartScene(int sceneId, string sceneType, System.Action onSuccess = null, System.Action onError = null)
        {
            if (sceneConnected)
            {
                Debug.unityLogger.LogError("NetPost", "在连接前请断开连接,当前连接Id为" + currentSceneId);
                return;
            }
            currentScene = GameObject.FindObjectOfType <NetScene>();
            if (currentScene == null)
            {
                Debug.unityLogger.LogError("NetPost", "未找到网络场景对象" + currentSceneId);
                return;
            }
            currentScene.OnBeforeConnect();

            currentSceneId = sceneId;
            if (currentServer == null)
            {
                currentServer = new BaseServer();
            }
            currentServer.Connect(ServerConst.ServerAddress, ServerConst.TcpRemotePort, ServerConst.UdpRemotePort, ServerConst.UdpLocalPort, true);

            CoroutineTaskManager.Instance.WaitSecondTodo(() =>
            {
                RequestSceneData reqData = new RequestSceneData();
                reqData.Id        = sceneId;
                reqData.SceneType = sceneType;
                currentServer.Request(HandlerConst.RequestId.RequsetSceneHandler, -1, reqData, SendType.TCP, (reqSceneRes) =>
                {
                    bool result = reqSceneRes.GetValue <BoolData>().Value;
                    if (result == true)
                    {
                        Debug.unityLogger.Log("请求场景成功并且注册至频道");
                        EventDispatcher.AddEventListener <NetBehavior>(NetSceneEvent.NetBehaviorAddToScene, AddNetBehavior);
                        EventDispatcher.AddEventListener <NetBehavior>(NetSceneEvent.NetBehaviorRemoveFromScene, RemoveNetBehavior);
                        sceneConnected = true;

                        if (onSuccess != null)
                        {
                            onSuccess();
                        }
                    }
                    else
                    {
                        Debug.unityLogger.LogError("NetPost", "请求场景失败");
                        if (onError != null)
                        {
                            onError();
                        }
                    }
                    currentScene.OnAfterConnect(result);
                }, () =>
                {
                    Debug.unityLogger.LogError("NetPost", "请求场景超时");
                    if (onError != null)
                    {
                        onError();
                    }
                });
            }, 0.5f);
        }
Exemplo n.º 4
0
 public PackageReciver(BaseServer server)
 {
     this.server = server;
 }