示例#1
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            _helper = Helper as INetworkHelper;
            _helper.LoadProtocolChannels(ChannelTypes);
            _helper.BeginConnectServerEvent += (cha) =>
            {
                BeginConnectServerEvent?.Invoke(cha);
            };
            _helper.ConnectServerSuccessEvent += (cha) =>
            {
                ConnectServerSuccessEvent?.Invoke(cha);
            };
            _helper.ConnectServerFailEvent += (cha) =>
            {
                ConnectServerFailEvent?.Invoke(cha);
            };
            _helper.DisconnectServerEvent += (cha) =>
            {
                DisconnectServerEvent?.Invoke(cha);
            };
            _helper.SendMessageEvent += (cha) =>
            {
                SendMessageEvent?.Invoke(cha);
            };
            _helper.ReceiveMessageEvent += (cha, mes) =>
            {
                ReceiveMessageEvent?.Invoke(cha, mes);
            };
        }
示例#2
0
        /// <summary>
        /// 接收消息(不需要保持连接的协议)
        /// </summary>
        private void ReceiveMessageNoConnect()
        {
            while (_isEnableThread)
            {
                INetworkMessage message = ReceiveMessage(Client);

                if (message != null)
                {
                    Main.Current.QueueOnMainThread(() =>
                    {
                        ReceiveMessageEvent?.Invoke(this, message);
                    });
                }
            }
        }
示例#3
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            //加载通信协议通道
            for (int i = 0; i < ChannelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(ChannelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!_protocolChannels.ContainsKey(type))
                        {
                            _protocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + ChannelTypes[i] + " 必须实现接口:IProtocolChannel!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + ChannelTypes[i] + "!");
                }
            }

            //初始化通道
            foreach (var channel in _protocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }
        /// <summary>
        /// 加载通信管道
        /// </summary>
        /// <param name="channelTypes">启用的通信协议通道类型</param>
        public void LoadProtocolChannels(List <string> channelTypes)
        {
            for (int i = 0; i < channelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(channelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!ProtocolChannels.ContainsKey(type))
                        {
                            ProtocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + channelTypes[i] + " 必须继承至基类:ProtocolChannelBase!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + channelTypes[i] + "!");
                }
            }

            foreach (var channel in ProtocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }
示例#5
0
        /// <summary>
        /// 接收消息(需要保持连接的协议)
        /// </summary>
        private void ReceiveMessageNeedConnect()
        {
            while (_isEnableThread)
            {
                if (IsConnect)
                {
                    INetworkMessage message = ReceiveMessage(Client);

                    if (message != null)
                    {
                        Main.Current.QueueOnMainThread(() =>
                        {
                            ReceiveMessageEvent?.Invoke(this, message);

                            if (IsDisconnectRequest(message))
                            {
                                DisconnectServer();
                            }
                        });
                    }
                }
            }
        }
 private void WebSocket_DataReceived(object sender, DataReceivedEventArgs e)
 {
     ReceiveMessageEvent?.Invoke(this, new ReceiveMessageArgs(e.Data));
 }