使用wcf的双工模式做的一个控制台聊天app
//wcf 服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract(CallbackContract = typeof(iMyclass))]
public interface IService1
{
[OperationContract]
string Send(string id,string pid, string str);
[OperationContract]
string Register(string id);
[OperationContract]
List<string> ALLhost();
}
[ServiceContract]
public interface iMyclass
{
[OperationContract(IsOneWay = true)]//回调函数方法必须加IsOneWay=true
void Reciver(string str);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public static Dictionary<string, iMyclass> hostdic;
public static List<string> allhost;
/// <summary>
///
/// </summary>
/// <param name="id">发送人</param>
/// <param name="pid">接受人</param>
/// <param name="str">内容</param>
/// <returns></returns>
public string Send(string id,string pid, string str)
{
try
{
foreach (var d in hostdic)
{
if (d.Key == pid)
{
d.Value.Reciver(id+"发送:"+str);
}
}
//iMyclass myclass= OperationContext.Current.GetCallbackChannel<iMyclass>();
//myclass.Reciver("你好");
return "1";
}
catch (Exception ex)
{
return ex.Message;
}
}
public List<string> ALLhost()
{
return allhost;
}
public string Register(string id)
{
if (hostdic == null)
{
hostdic = new Dictionary<string, iMyclass>();
}
if (allhost == null)
{
allhost = new List<string>();
}
iMyclass imyclass = OperationContext.Current.GetCallbackChannel<iMyclass>();
hostdic.Add(id, imyclass);
allhost.Add(id);
return id;
}
}
}
//宿主
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ServerHost
{
class Program
{
static void Main(string[] args)
{
ServiceHost serverhost = new ServiceHost(typeof(WcfService1.Service1));
serverhost.Open();
Console.WriteLine("open");
Console.ReadKey();
}
}
}
//宿主配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfService1.Service1"> <endpoint address="net.tcp://192.168.1.12:3721/calculatorservice"
//改为本地的ip
binding="netTcpBinding" contract="WcfService1.IService1" bindingConfiguration ="TicketBindingConfiguration"/> </service> </services> <bindings> <netTcpBinding> <binding name="TicketBindingConfiguration" openTimeout="00:10:10" receiveTimeout="00:10:10" sendTimeout="00:10:10" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> <readerQuotas maxStringContentLength="6553600" maxArrayLength="6553600" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
//客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfService1;
using System.ServiceModel;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
myclass myclass = new myclass();
// myclass.contentevent += receive;
InstanceContext callback = new InstanceContext(myclass);
// ChannelFactory<IService1> channl = new ChannelFactory<IService1>( "wcfserver"); 如果不是双工模式没有回调的话就使用这个
DuplexChannelFactory<IService1> channl = new DuplexChannelFactory<IService1>(callback, "wcfserver");
IService1 IService1 = channl.CreateChannel();
Console.WriteLine("请输入自己的用户名");
string id = Console.ReadLine();
string str = IService1.Register(id.Trim());
Console.WriteLine(str);
while (true)
{
Console.WriteLine("输入发送数据");
string info = Console.ReadLine();
Console.WriteLine("输入接受人");
string piduser = Console.ReadLine();
Console.WriteLine("发送给" + piduser.Trim() + ":" + info.Trim());
IService1.Send(id,piduser.Trim(), info.Trim());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
public class myclass : iMyclass
{
// public delegate void conten(string str);
// public event conten contentevent;
public void Reciver(string str)
{
Console.WriteLine("{0}:" + str, System.DateTime.Now);
// contentevent(str);
}
}
}
//客户端配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="wcfserver" address="net.tcp://192.168.1.12:3721/calculatorservice" //改为本地的ip binding="netTcpBinding" contract="WcfService1.IService1" bindingConfiguration ="TicketBindingConfiguration"/> </client> <bindings> <netTcpBinding> <binding name="TicketBindingConfiguration" openTimeout="00:10:10" receiveTimeout="00:10:10" sendTimeout="00:10:10" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> <readerQuotas maxStringContentLength="6553600" maxArrayLength="6553600" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
使用wcf的双工模式做的一个控制台聊天app的更多相关文章
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
原文链接 : Create an iOS Chat App using JSQMessagesViewController – Part 2 原文作者 : Mariusz Wisniewski 译者 ...
- 利用WCF双工模式实现即时通讯
概述 WCF陆陆续续也用过多次,但每次都是浅尝辄止,以将够解决问题为王道,这几天稍闲,特寻了些资料看,昨晚尝试使用WCF的双工模式实现了一个简单的即时通讯程序,通过服务端转发实现客户端之间的通讯.这只 ...
- WCF学习之旅—HTTP双工模式(二十)
WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统
何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况. 心跳监控的目的是什么? 与医院里面的心跳监视仪目的类似,监控程序运行状态 ...
- 利用WCF的双工通讯实现一个简单的心跳监控系统 z
利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...
- WCF消息交换模式之双工通讯(Duplex)
WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...
- WCF学习之旅—TCP双工模式(二十一)
WCF学习之旅—请求与答复模式和单向模式(十九) WCF学习之旅—HTTP双工模式(二十) 五.TCP双工模式 上一篇文章中我们学习了HTTP的双工模式,我们今天就学习一下TCP的双工模式. 在一个基 ...
- [SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端
原文:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端 之前开发基于WinForm监控的软件,服务端基于Wcf实现,里面涉及双工模式,在客户端里面,采用心跳包机制保持与服 ...
- WCF服务创建与使用(双工模式)
昨天发布了<WCF服务创建与使用(请求应答模式)>,今天继续学习与强化在双工模式下WCF服务创建与使用,步骤与代码如下. 第一步,定义服务契约(Service Contract),注意Se ...
随机推荐
- python 协程库gevent学习--源码学习(一)
总算还是要来梳理一下这几天深入研究之后学习到的东西了. 这几天一直在看以前跟jd对接的项目写的那个gevent代码.为了查错,基本上深入浅出了一次gevent几个重要部件的实现和其工作的原理. 这里用 ...
- linux 源的配置更新
Ubuntu 首先编辑sources.list这个文件 vi /etc/apt/sources.list 把sources.list文件内容替换成如下 deb http://mirrors.aliyu ...
- codeforces1045B
CF1045B 自己瞎鸡巴yy了一下,可知若一个数X不能被表示出来,那么X所有的表示方法都在A集合中,如a1,a2,a3······an-1,an-2中若a1+ai不能被表示出来,那么如果a1到ai是 ...
- SESSION和cookie的使用和区别
PHP中SESSION和cookie的使用和区别 cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP在http协议的头信息里发送cookie, 因此 setcookie( ...
- [IOI2018]排座位——线段树
题目链接: IOI2018seat 题目大意:给出一个$H*W$的矩阵,将$0 \sim W*H-1$分别填入矩阵的格子里(每个格子里一个数),定义一个子矩阵是美妙的当且仅当这个子矩阵包含且仅包含$0 ...
- BZOJ3298[USACO 2011Open]cow checkers——威佐夫博弈
题目描述 一天,Besssie准备和FJ挑战奶牛跳棋游戏.这个游戏上在一个M*N的棋盘上, 这个棋盘上在(x,y)(0<=x棋盘的左下角是(0,0)坐标,棋盘的右上角是坐标(M-1,N-1). ...
- BZOJ4372 烁烁的游戏(动态点分治+线段树)
建出点分树,每个节点维护其作为点分树上lca对子树内点的贡献,线段树维护即可,同时另开一个线段树以减掉父亲重复的贡献. #include<iostream> #include<cst ...
- Vue模板 script部分
<script> export default { name: "Home", data() { return {}; }, methods: { // 组件的方法 } ...
- poj 2236 Wireless Network (并查集)
链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...
- day24 异常处理
程序一旦发生错误,就从错误的位置停下不在执行后面的内容一般可能预估但是无法处理的问题可以用异常处理进行操作异常处理后会继续执行后面的代码 try: # 写在try中的语句是一定执行的 ret = in ...