/// <summary> /// 发起消息订阅并且指定服务器端的回调函数 /// </summary> ///<param name="reqSrvUrl">服务的地址,必须是Publish模式</param> ///<param name="resultType">服务返回的对象类型</param> ///<param name="action">自定义的消息处理方法</param> /// <param name="function">服务器的回调函数</param> /// <returns></returns> private int Subscribe(string reqSrvUrl, Type resultType, Action <string> action, MyFunc <string, string> function) { if (this.Connect()) { //subServiceList.Add(new SubscribeArgs() { ReqSrvUrl = reqSrvUrl, ResultType = resultType, CallBackAction = action }); return(ServiceSubscriber.SendMessage(reqSrvUrl, resultType, action, function, null)); } else { return(0); } }
/// <summary> /// 以当前连接(如未打开则打开),异步请求并处理数据 /// </summary> /// <typeparam name="T">请求的数据类型</typeparam> /// <param name="request">服务请求对象</param> /// <param name="resultDataType">数据类型</param> /// <param name="action">要异步执行的委托方法</param> public void RequestServiceCurrentSubscribe <T>(ServiceRequest request, DataType resultDataType, Action <T> action) { //if (sub == null || sub.Closed) // throw new InvalidOperationException("未打开连接或者连接已经关闭,请先调用Connect 方法"); CheckConnect(); ServiceSubscriber.SendMessage(request.ServiceUrl, null, (remoteMsg) => { //MessageConverter<T> convert = new MessageConverter<T>(remoteMsg, resultDataType); //if (action != null) // action(convert.Result); // 上面代码已经优化 ProcessRemoteMessage <T>(remoteMsg, resultDataType, null, action); }); }
/// <summary> /// 向服务器发送消息,当服务器处理完后,会回调当前提供的回调方法 /// </summary> /// <param name="message">发给服务器的消息</param> /// <param name="ResultType">结果的对象类型</param> /// <param name="action">客户端的回调方法</param> /// <param name="function">在执行过程中的回调函数</param> /// <param name="preFunction">在执行过程前的回调函数</param> /// <returns>消息标识</returns> public int RequestService(string message, Type ResultType, Action <string> action, MyFunc <string, string> function, MyFunc <string, string> preFunction) { return(ServiceSubscriber.SendMessage(message, ResultType, action, function, preFunction)); }