wcf双工通讯服务器给客户端发消息速度慢,要等95秒
这是服务端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
//Interface contracts in the IService1.cs file.
namespace DuplexService
{
[ServiceContract(Namespace = "Silverlight", CallbackContract = typeof(IDuplexClient))]
public interface IDuplexService
{
[OperationContract]
void Order(string name, int quantity);
}
[ServiceContract]
public interface IDuplexClient
{
[OperationContract(IsOneWay = true)]
void Receive(Order order);
}
public class Order
{
public OrderStatus Status { get; set; }
public List<string> Payload { get; set; }
}
public enum OrderStatus
{
Processing,
Completed
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
namespace DuplexService
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class OrderService : IDuplexService
{
string orderName;
int orderQuantity;
private static List<IDuplexClient> channelsList = new List<IDuplexClient>();
private static Object thisLock = new Object();
public void Order(string name, int quantity)
{
orderName = name;
orderQuantity = quantity;
IDuplexClient client1 = OperationContext.Current.GetCallbackChannel<IDuplexClient>();
channelsList.Add(client1);
OperationContext.Current.Channel.Closed += new EventHandler(ShowOffLine);
Order order = new Order();
order.Payload = new List<string>();
order.Payload.Add("在线人数[" + channelsList.Count.ToString()+"]");
order.Status = OrderStatus.Completed;
foreach (IDuplexClient client2 in channelsList)
{
client2.Receive(order);
}
}
private void ShowOffLine(object sender, EventArgs e)
{
IDuplexClient callback = sender as IDuplexClient;
channelsList.Remove(callback);
Order order = new Order();
order.Payload = new List<string>();
order.Payload.Add("在线人数[" + channelsList.Count + "]");
order.Status = OrderStatus.Completed;
foreach (IDuplexClient client1 in channelsList)
{
client1.Receive(order);
}
}
}
}