/// <summary> /// 接收消息 /// </summary> /// <param name="proxySocket"></param> /// <param name="message"></param> void proxy_MessageReceived(ZProxySocket proxySocket, ZMessage message) { if (MessageReceived != null) { MessageReceived(proxySocket, message); //激发MessageReceived事件 } }
/// <summary> /// 客户端断开 /// </summary> /// <param name="proxySocket"></param> void proxy_DisConnected(ZProxySocket proxySocket) { if (DisConnected != null) { DisConnected(proxySocket); //激发DisConnected事件 } }
/// <summary> /// 侦听回调方法 /// </summary> /// <param name="ar"></param> private void OnAccept(IAsyncResult ar) { try { Socket new_accept = _socket.EndAccept(ar); //新连接socket ZProxySocket proxy = GetProxy(new_accept); //创建代理socket proxy.DisConnected += new DisConnectedEventHandler(proxy_DisConnected); proxy.MessageReceived += new MessageReceivedEventHandler(proxy_MessageReceived); proxy.StartReceive(); //代理socket开始接收数据 if (Connected != null) { Connected.BeginInvoke(proxy, null, null); //激发Connected事件 } _socket.BeginAccept(new AsyncCallback(OnAccept), null); //开始下一次异步侦听 } catch { } }
/// <summary> /// 异步连接服务器回调方法 /// </summary> /// <param name="ar"></param> private void OnConnect(IAsyncResult ar) { try { _socket.EndConnect(ar); _proxy = GetProxy(_socket); //创建代理socket _proxy.DisConnected += new DisConnectedEventHandler(_proxy_DisConnected); _proxy.MessageReceived += new MessageReceivedEventHandler(_proxy_MessageReceived); _proxy.StartReceive(); //开启接收数据 _connected = true; if (Connected != null) //成功 激发Connected事件 { Connected(_proxy); } } catch { if (Connected != null) //失败 激发Connected事件 { Connected(null); } } }