using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service.Interface
{
[ServiceContract(CallbackContract = typeof(ICallBack))]
public interface INoticeOperator
{
[OperationContract]
void Register(String id);
[OperationContract]
void UnRegister(String id);
[OperationContract]
void SendMessage(String from, String to, String message);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service.Interface
{
public interface ICallBack
{
[OperationContract(IsOneWay = true)]
void Notice(String message);
}
}
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class Client
{
public String Id { get; set; }
public ICallBack CallBack { get; set; }
}
}
using Models;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Service
{
public class NoticeOperator : INoticeOperator
{
private static List<Client> clientList = new List<Client>();
public void Register(string id)
{
Console.WriteLine("register:" + id);
ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>();
clientList.Add(new Client() { Id = id, CallBack = callBack });
}
public void UnRegister(string id)
{
Console.WriteLine("unRegister:" + id);
Client client = clientList.Find(c => c.Id == id);
if (client != null)
{
clientList.Remove(client);
}
}
public void SendMessage(string from, string to, string message)
{
Client client = clientList.Find(c => c.Id == to);
if (client != null)
{
String longMessage = String.Format("message from {0} to {1} at {2} : {3}", from, to, DateTime.Now.ToString("HH:mm:ss"), message);
Console.WriteLine(longMessage);
client.CallBack.Notice(longMessage);
}
}
}
}
using Service;
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(NoticeOperator)))
{
host.AddServiceEndpoint(typeof(INoticeOperator), new NetTcpBinding(), "net.tcp://127.0.0.1:9527/NoticeOperator");
host.Opened += (s, e) => Console.WriteLine("service is running...");
host.Open();
Console.ReadLine();
}
}
}
}
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class CallBack : ICallBack
{
public void Notice(string message)
{
Console.WriteLine(message);
}
}
}
using Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
InstanceContext context = new InstanceContext(new CallBack());
using (ChannelFactory<INoticeOperator> factory = new DuplexChannelFactory<INoticeOperator>(context, new NetTcpBinding(), "net.tcp://127.0.0.1:9527/NoticeOperator"))
{
INoticeOperator proxy = factory.CreateChannel();
String selfId = args[0];
String friendId = args[1];
proxy.Register(selfId);
Console.WriteLine("----------Register------------");
while(true)
{
String message = Console.ReadLine();
if (message == "q")
{
proxy.UnRegister(selfId);
break;
}
else
{
proxy.SendMessage(selfId, friendId, message);
}
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有