C# WCF实现聊天室功能
1、WCF是什么
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架
看这篇文章之前,可以先看我的另一篇文章,初步了解一下WCF:https://www.cnblogs.com/wskxy/p/9435300.html
2、如何通过WCF做聊天室
首先,我们画个示意图:

按照图示,我们来建立Service和Client
客户端选择WinForm,这是不用争议的
服务端为啥要选用Web呢,这是为了方便IIS配置
3、首先是ServiceWeb

IUserService.cs 定义好接口(IUserService)和关联回调(IUserCallback)
using System.ServiceModel; namespace ServiceWeb
{
//信息类,懒得新建,放一起了
[ServiceContract]
public class UserInfo
{
public string acc;
public string sayContext;
}
[ServiceContract]
public class UserInfoSubscriber : UserInfo
{
public IUserCallback subscriber;
} //回调Client函数接口
public interface IUserCallback
{
[OperationContract(IsOneWay = true)]
void OnLogin(UserInfo user);
[OperationContract(IsOneWay = true)]
void OnQuit(UserInfo user);
[OperationContract(IsOneWay = true)]
void OnSay(UserInfo user);
}
//Service服务函数接口
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IUserCallback))]
public interface IUserService
{
[OperationContract(IsOneWay = true)]
void Login(string acc, string pwd);
[OperationContract(IsOneWay = true)]
void Quit(string acc);
[OperationContract(IsOneWay = true)]
void Say(UserInfo user);
}
}
UserService.svc 实现接口函数
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel; namespace ServiceWeb
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“User”。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class UserService : IUserService
{
/*存储登录信息*/
/*必须加static,不然每次都被重新实例化*/
public static List<UserInfoSubscriber> userList = new List<UserInfoSubscriber>();
public void Login(string acc, string pwd)
{
/*1、检测登录,在数据库判断登录信息,不涉及本章重点,为方便代码省略,默认成功*/ /*2、成功登录后广播*/
//为当前用户创建订阅者(回调接口)
IUserCallback subscriber = OperationContext.Current.GetCallbackChannel<IUserCallback>(); if (userList.FindIndex(x => x.acc == acc) == -1)//避免重复登录
{
var user = new UserInfoSubscriber()
{
acc = acc,
subscriber = subscriber
};
userList.Add(user);
//user.subscriber.OnLogin(user);//用子类作为参数无效,所以需要重新new返回信息(可能和WCF协议有关)
//user.subscriber.OnLogin((UserInfo)user);//这样也不行
user.subscriber.OnLogin(new UserInfo() { acc = acc });//成功执行回调
foreach (UserInfoSubscriber item in userList.Where(x => x.acc != acc)) //告知其他客户端当前用户已上线
{
item.subscriber.OnLogin(new UserInfo() { acc = acc });
}
}
} public void Quit(string acc)
{
var user = userList.Find(x => x.acc == acc);
if (user != null)
{
user.subscriber.OnQuit(new UserInfo() { acc = acc });
userList.Remove(user);
}
} public void Say(UserInfo _user)
{
var user = userList.Find(x => x.acc == _user.acc);
foreach (UserInfoSubscriber item in userList)
{
item.subscriber.OnSay(_user);
}
} }
}
注意 Web.config 的<services>,需要有User服务(有的不会自动建立,然后产生错误,缺了就补上)
<services>
<service name="ServiceWeb.UserService">
<!--问题:协定需要会话,但是绑定“BasicHttpBinding”不支持它或者因配置不正确而无法支持它-->
<!--解决:将BasicHttpBinding换成wsDualHttpBinding-->
<endpoint address="" binding="wsDualHttpBinding" contract="ServiceWeb.IUserService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/Service/UserService/"/>
</baseAddresses>
</host>
</service>
</services>
这样,Service就算定义好了,可以直接配置到IIS,打开svc可以看到:

4、Client

先把服务引用进来,引用后配置文件有如下信息:
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IUserService" />
</wsDualHttpBinding>
</bindings>
<client>
<endpoint
address="http://10.163.101.252:7001/UserService.svc"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IUserService"
contract="VWCFUserService.IUserService" name="WSDualHttpBinding_IUserService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
然后,让我们开心写界面

代码如下:
using Client.VWCFUserService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Client
{
public partial class Form1 : Form, IUserServiceCallback
{
InstanceContext context;
UserServiceClient sc; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.tb_Acc.Text = "kxy";
this.tb_Pwd.Text = "123";
this.tb_Message_Box.ReadOnly = true ;
this.btn_Quit.Enabled = false;
this.btn_Send.Enabled = false;
//this.tb_Send_Box.Focus(); //这个不知道为啥无效
this.ActiveControl = this.tb_Send_Box;
}
private void Login_Click(object sender, EventArgs e)
{
context = new InstanceContext(this);
sc = new UserServiceClient(context);
//sc.Login(sc.InnerChannel.SessionId, this.tbAcc.Text);
sc.Login(this.tb_Acc.Text, this.tb_Pwd.Text);
} private void Quit_click(object sender, EventArgs e)
{
if (sc != null)
sc.Quit(this.tb_Acc.Text);
} private void send_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tb_Send_Box.Text))
{
sc.Say(new UserInfo() { acc = this.tb_Acc.Text, sayContext = this.tb_Send_Box.Text });
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (sc != null)
sc.Quit(this.tb_Acc.Text);
} public void OnLogin(VWCFUserService.UserInfo user)
{
tb_Message_Box.Text += $"\r\n已登录:{user.acc}";
this.tb_Acc.ReadOnly = true;
this.tb_Pwd.ReadOnly = true;
this.btn_Login.Enabled = false;
this.btn_Quit.Enabled = true;
this.btn_Send.Enabled = true;
}
public void OnQuit(VWCFUserService.UserInfo user)
{
tb_Message_Box.Text += $"\r\n已退出:{user.acc}";
this.tb_Acc.ReadOnly = false;
this.tb_Pwd.ReadOnly = false;
this.btn_Login.Enabled = true;
this.btn_Quit.Enabled = false;
this.btn_Send.Enabled = false;
}
public void OnSay(VWCFUserService.UserInfo user)
{
tb_Message_Box.Text += $"\r\n{user.acc}-{DateTime.Now.ToString("f")}";
tb_Message_Box.Text += $"\r\n{user.sayContext}";
this.tb_Send_Box.Text = "";
this.tb_Send_Box.Focus(); }
}
}
到这里,我们的通讯就可以正常进行了。
请大家自己尝试,可以打开多个进程,体验多人在线!!!
5、Client的发布和版本更新
WinForm怎么进行发布和检测更新呢?
也是利用IIS!!!
具体操作如下:
首先将WinForm发布打包(一步一步截图太麻烦,直接看最后信息呈现)

参数解释:
1、该应用程序将发布到(发布生产程序存放的位置,本地随便都行)
2、用户将从以下位置启动此应用程序(这个地址是后面程序将要在IIS上的host地址)
生成文件如下:

将文件发布到服务器(服务器文件位置随便),定义站点10.163.101.252:7002
访问http://10.163.101.252:7002/publish.htm,可以进行Client下载安装

并且每次启动Client的时候,会自动检查是否有版本更新
(当然需要发布新版本,并将新程序Copy到站点,才会进行更新)
C# WCF实现聊天室功能的更多相关文章
- Netty学习笔记(四) 简单的聊天室功能之服务端开发
前面三个章节,我们使用了Netty实现了DISCARD丢弃服务和回复以及自定义编码解码,这篇博客,我们要用Netty实现简单的聊天室功能. Ps: 突然想起来大学里面有个课程实训,给予UDP还是TCP ...
- 使用epoll实现聊天室功能,同时比较epoll和select的异同
1.首先介绍一下select和epoll的异同,如下(摘抄自https://www.cnblogs.com/Anker/p/3265058.html) select的几大缺点: (1)每次调用sele ...
- [Python] socket发送UDP广播实现聊天室功能
一.说明 本文主要使用socket.socket发送UDP广播来实现聊天室功能. 重点难点:理解UDP通讯流程.多线程.UDP广播收发等. 测试环境:Win10\Python3.5. 程序基本流程:创 ...
- SignalR实现在线聊天室功能
一.在线聊天室 1.新建解决方案 SignalROnlineChatDemo 2.新建MVC项目 SignalROnlineChatDemo.Web (无身份验证) 3.安装SignalR PM> ...
- 03_netty实现聊天室功能
[概述] 聊天室主要由两块组成:聊天服务器端(ChatRoomServer)和聊天客户端(ChatClient). [ 聊天服务器(ChatRoomServer)功能概述 ] 1.监听所有客户端的接入 ...
- PHP 之websocket实现聊天室功能
一.功能界面 具体的详细代码:https://github.com/yangsphp/websocket-master/tree/master 二.具体代码实现 1.前端代码如下 <!DOCTY ...
- 黑科技!仅需 3 行代码,就能将 Gitter 集成到个人网站中,实现一个 IM 即时通讯聊天室功能?
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- java web利用mvc结构实现简单聊天室功能
简单聊天室采用各种内部对象不适用数据库实现. 一个聊天室要实现的基本功能是: 1.用户登录进入聊天室, 2.用户发言 3.用户可以看见别人发言 刚才算是简单的需求分析了,现在就应该是进 ...
- 通过WebSocket实现一个简单的聊天室功能
WebSocket WebSocket是一个协议,它是是基于TCP的一种新的网络协议,TCP协议是一种持续性的协议,和HTTP不同的是,它可以在服务器端主动向客户端推送消息.通过这个协议,可以在建立一 ...
- Netty学习笔记(六) 简单的聊天室功能之WebSocket客户端开发实例
在之前的Netty相关学习笔记中,学习了如何去实现聊天室的服务段,这里我们来实现聊天室的客户端,聊天室的客户端使用的是Html5和WebSocket实现,下面我们继续学习. 创建客户端 接着第五个笔记 ...
随机推荐
- 单一接口优化过程全记录(主要涉及Redis)
接口优化过程记录 问题背景 某个接口耗时长(247ms),但里面逻辑不算复杂,只进行了简单的对象引用以及操作了多次Redis 步骤1:链路追踪,确定业务耗时点 接口里通过链路追踪以及日志查询发现主要是 ...
- UBOOT 启动流程
一.概述 uboot 的启动流程在网上有很多大佬记录,但是了对于像我这样的新手就有些困难了,而我也不做 uboot 相关的工作,所以没必去研究代码,这里我特意整理了一下,以流程图的形式展现代码执行的流 ...
- 如何基于 Redis 实现分布式锁
什么是分布式锁 分布式锁:不同进程必须以互斥方式使用共享资源的一种锁方法实现. 实现分布式锁的基础 互斥.任何时刻,只有一个客户端持有锁. 无死锁.最终总是有可能获得锁,即使持有锁的客户端已经崩溃. ...
- python中使用pip 安装第三方库报错归类及解决方式
1. 离线安装virtualenv报错,安装命令:python setup.py install 解决方式:升级setuptools 2. 安装第三方库时安装失败,安装命令:pip install ...
- 编译安装nmon
nmon 是什么? nmon(Nigel's performance Monitor for Linux)是一种Linux性能监视工具,当前它支持 Power/x86/x86_64/Mainframe ...
- 利用Git同步思源笔记
旧文章从语雀迁移过来,原日期为2022-10-22 思源笔记是一款优秀的本地优先的双链大纲笔记软件,拥有强大的笔记编辑功能且都是免费,唯一付费的就是云同步等一些服务了.但如果暂时还用不着云同步的,我们 ...
- 1+x初级Web的关键词填写
H5+CSS: 声明HTML网页标准:<!DOCTYPE> 图片标签 img css颜色样式color 定位 position 绝对absolute 相对 relative 外边距:mar ...
- .NET 6配置EF Core数据库链接字符串
appsetting.json { "Logging": { "LogLevel": { "Default": "Informat ...
- ArcGIS工具 - 导出数据库结构
为了保证数据的一致性,数据库结构的正确性在数据库建设和管理过程中显示十分重要,在各个地理信息类项目的技术规定中都对空间数据库的结构进行明确和详细的定义,有时为了方便检查或文档编辑需要将数据结构导出,为 ...
- Java学习笔记:2022年1月11日
Java学习笔记:2022年1月11日 摘要:这篇笔记主要讲解了一些数据在计算机中的存在方式相关的知识点,并由此延伸出了数据在计算机中的操作以及一些数据结构的知识. @ 目录 Java学习笔记:2 ...