none
wcf silverlight 双向通信 客户端关闭或刷新 服务器端的回调超时 不能及时通知其他客户端 RRS feed

  • 问题

  • 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]是缓存的通道 当客户端关闭后 服务推送会超时 服务卡在这 这样不能及时的通知其他用户此用户离开,希望在推送失败的时候,修改此通道的标识,以实现上线 下线通知所有人的目的 
     本人初学 有没有什么好的解决方案 麻烦大家了

    2010年4月6日 7:31

答案

全部回复