/// <summary> /// 从网络管理器中移除当前脚本绑定的响应事件 /// </summary> private void DisableHandlers() { foreach (KeyValuePair <ProtoID, NetworkEventHandler> pair in m_HandlerPairs) { NetworkChangeManager.Unregister(pair.Key, pair.Value); } }
/// <summary> /// 添加脚本对象要处理的网络协议Id及其响应方法 /// </summary> /// <param name="id">协议Id</param> /// <param name="handler">进行响应的方法</param> protected void AddNetworkHandler(ProtoID id, NetworkEventHandler handler) { m_HandlerPairs.Add(new KeyValuePair <ProtoID, NetworkEventHandler> (id, handler)); // 如果脚本处于启用状态,要实时向网络管理器注册事件响应 if (this.enabled) { NetworkChangeManager.RegisterWeak(id, handler); } }
/// <summary> /// 交给Command,这里不想关心发给谁。 /// </summary> void Update() { // 在网络Session池中取消息 if (mTcpServerSessionId > 0) { while ((m_proto = networkCtrl.GetMessage(mTcpServerSessionId)) != null) { try { Debug.Log("server network change manager recv " + m_proto.GetID().ToUInt16()); // 处理消息事件 NetworkChangeManager.Notify(m_proto.GetID(), m_proto); // 移除超时回调 RemoveTimeOutHandlers(m_proto.GetID()); } catch (Exception exp) { Debug.LogException(exp); } } } if (mTcpClientSessionId > 0) { // 在网络Session池中取消息 while ((m_proto = networkCtrl.GetMessage(mTcpClientSessionId)) != null) { try { Debug.Log("client network change manager recv " + m_proto.GetID().ToUInt16()); // 处理消息事件 NetworkChangeManager.Notify(m_proto.GetID(), m_proto); // 移除超时回调 RemoveTimeOutHandlers(m_proto.GetID()); } catch (Exception exp) { Debug.LogException(exp); } } } }
public void RegisterShortCallBack(UInt16 _protoId, NetworkEventHandler _callback) { NetworkChangeManager.RegisterStrong((ProtoID)_protoId, _callback); }
public void RegisterCallBack(ProtoID _protoId, NetworkEventHandler _callback) { NetworkChangeManager.RegisterWeak(_protoId, _callback); }