积极答复者
wcf silverlight 双向通信 客户端关闭或刷新 服务器端的回调超时 不能及时通知其他客户端

问题
-
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Channels; using System.Threading; using System.IO; namespace DuplexService { // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file. public class OrderService : IDuplexService { IDuplexClient client; static List<IDuplexClient> clientList = new List<IDuplexClient>(); static List<int> usermap = new List<int>(); static List<int> leave = new List<int>(); static int sum = 0; static Timer _checker = null; static Message returnMessage; public static void Check() { //每3秒检查一次 _checker = new Timer(new TimerCallback(SpollingClients), null, 0, 3000); } static void SpollingClients(object data) { try { for (int i = 0; i < usermap.Count;i++ ) { if (usermap[i]<= 0) { if (!leave.Contains(i)) leave.Add(i); continue; } } foreach (int z in leave) { for (int i = 0; i < usermap.Count; i++) { if (usermap[i] > 0) { BroadCast(i, z + "离开游戏"); } Thread.Sleep(100); } } } catch(Exception ex) { // 出错则记日志 using (StreamWriter sw = new StreamWriter(@"C:\Silverlight_Duplex_Log.txt", true)) { sw.Write(ex.ToString()); sw.WriteLine(); } } } public void Order(Message receivedMessage) { client = OperationContext.Current.GetCallbackChannel<IDuplexClient>(); returnMessage = Message.CreateMessage(MessageVersion.Soap11, "Silverlight/IDuplexService/Receive", "欢迎进入胡椒游戏世界"); client.Receive(returnMessage); clientList.Add(client); usermap.Add(1); sum++; for (int j = 0; j < usermap.Count; j++) { if (usermap[j] > 0) BroadCast(j, sum + "进入游戏"); Thread.Sleep(100); } } static void BroadCast(int x, string word) { returnMessage = Message.CreateMessage(MessageVersion.Soap11, "Silverlight/IDuplexService/Receive", word); try { if (usermap[x] > 0) clientList[x].Receive(returnMessage);//clientList[x]是缓存的通道 当客户端关闭后 服务推送超时 } catch(Exception ex) { usermap[x] = 0; // 出错则记日志 using (StreamWriter sw = new StreamWriter(@"C:\Silverlight_Duplex_Log.txt", true)) { sw.Write(ex.ToString()); sw.WriteLine(); } } } } }
if (usermap[x] > 0) clientList[x].Receive(returnMessage);
clientList[x]是缓存的通道 当客户端关闭后 服务推送会超时 服务卡在这 这样不能及时的通知其他用户此用户离开,希望在推送失败的时候,修改此通道的标识,以实现上线 下线通知所有人的目的
本人初学 有没有什么好的解决方案 麻烦大家了
答案
-
服务可以做个try catch处理。
另外 客户端下线,服务必须知道吗?
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum- 已标记为答案 Ace2011 2010年4月7日 2:24
全部回复
-
服务可以做个try catch处理。
另外 客户端下线,服务必须知道吗?
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum- 已标记为答案 Ace2011 2010年4月7日 2:24
-
研究了一下 确实没办法解决这个问题 轮询超时
我没做过测试,不过在双工通信模式下,常常会出现客户端调用死锁的状态,无法等到服务的响应。
UseSynchronistaionContext=TRue 会阻塞客户端进程。也会导致异常,你可以看一下是不是这个原因,以前有人遇到类似的问题。老外发过一个帖子。
http://social.microsoft.com/Forums/zh-CN/wcfzhchs/thread/7bff4dab-0297-4328-ae22-867e6387b48b
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum