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实现,下面我们继续学习. 创建客户端 接着第五个笔记 ...
随机推荐
- MIsc writeup
1. 杂项 图片里面有什么 ,附件为一张图片 通过Binwalk查看发现有压缩包,通过foremost分离一下. 打开输出文件,发现里面有两个图片. 00000000.png是原图,00000722. ...
- openpyxl写数据
import osimport openpyxlos.chdir(r'D:/openpyxl') wb = openpyxl.Workbook() sht = wb.create_sheet('dat ...
- 痞子衡嵌入式:低功耗&高性能边缘人工智能应用的新答案 - MCXN947
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦MCX系列MCU的新品MCXN947. 自 2015 年恩智浦和飞思卡尔合并成新恩智浦之后,关于它们各自的 Arm Cortex- ...
- Redis基本操作(2)
一.list类型 列表的元素类型为string 按照插⼊顺序排序 增加.修改 例1:在左侧插⼊数据 lpush key value1 value2 ... 例2:在右侧插⼊数据 rpush key v ...
- 【转载】EXCEL VBA 关于范围选择代码集
Range("A1:B2").Select '选中"A1"."A2"."B1"."B2"四个连续的单 ...
- TypeScript 前端工程最佳实践
作者:王春雨 前言 随着前端工程化的快速发展, TypeScript 变得越来越受欢迎,它已经成为前端开发人员必备技能. TypeScript 最初是由微软开发并开源的一种编程语言,自2012年10月 ...
- [编程基础] Python对象的浅拷贝与深拷贝笔记
Python中的赋值语句不创建对象的副本,它们只将名称绑定到对象.对于不可变的对象,这通常没有什么区别.但是对于处理可变对象或可变对象的集合,您可能需要寻找一种方法来创建这些对象的"真实副本 ...
- Web3区块链DAS域名注册教程 tron trx链波卡钱包地址解析 用户名转账 ENS
而在去中心化系统中,大部分人充值.转账时,使用的都是区块链原生的长地址,比如: ETH 的地址: 0x9euo8sHip*******dHld90 CKB 的地址: ckHUEI829D******* ...
- C语言写的 史上最公平的投票系统
#include<stdio.h> #include<string.h> #define MMM 4 struct student { char name[10]; int c ...
- 全志V3S 调试串口更改或关闭
有时项目外设比较多,很容易造成串口不够用的情况. 最近就遇到了,新增加一个GPS模块串口的,串口现在外部只有原来的调试串口可以用,所以 尝试将调试口更改为普通串口. 经过网上看大神们的文章和自己摸索, ...