C# WebSocket
(1)建立在 TCP 协议之上,服务器端的实现比较容易。
(2)与 HTTP 协议有着良好的兼容性。默认端口也是80和443,并且握手阶段采用 HTTP 协议,因此握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
(3)数据格式比较轻量,性能开销小,通信高效。
(4)可以发送文本,也可以发送二进制数据。
(5)没有同源限制,客户端可以与任意服务器通信。
(6)协议标识符是ws(如果加密,则为wss),服务器网址就是 URL。
.Net 4.5及以后的版本才提供对websocket的支持,若项目所基于的.Net版本低于.Net 4.5且高于.Net 3.5,不妨尝试采用开源库websocket-sharp。
GitHub路径如下:https://github.com/sta/websocket-sharp 和 https://github.com/statianzo/Fleck
下面引用到 Fleck 和 WebSocketSharpFork
客户端
/// <summary>
/// 客户端帮助类 作者:韩永健
/// </summary>
public class WebSocketClientHelper
{
public delegate void ActionEventHandler();
public delegate void MessageEventHandler(string message);
public delegate void ErrorEventHandler(Exception ex);
/// <summary>
/// 客户端打开连接时调用
/// </summary>
public event ActionEventHandler OnOpen;
/// <summary>
/// 客户端关闭连接时调用
/// </summary>
public event ActionEventHandler OnClose;
/// <summary>
/// 收到客户端信息时调用
/// </summary>
public event MessageEventHandler OnMessage;
/// <summary>
/// 执行错误时调用
/// </summary>
public event ErrorEventHandler OnError;
/// <summary>
/// 服务
/// </summary>
private WebSocket client;
/// <summary>
/// 服务URL
/// </summary>
public string ServerUtl { private set; get; }
/// <summary>
/// 是否重连
/// </summary>
public bool IsReConnection { set; get; }
/// <summary>
/// 重连间隔(毫秒)
/// </summary>
public int ReConnectionTime { set; get; }
public WebSocketClientHelper(string serverUtl)
{
this.ServerUtl = serverUtl;
client = new WebSocket(this.ServerUtl);
client.OnOpen += new EventHandler(client_OnOpen);
client.OnClose += new EventHandler<CloseEventArgs>(client_OnClose);
client.OnMessage += new EventHandler<MessageEventArgs>(client_OnMessage);
client.OnError += new EventHandler<ErrorEventArgs>(client_OnError);
}
public WebSocketClientHelper(string serverUtl, bool isReConnection, int reConnectionTime = 1000)
: this(serverUtl)
{
this.IsReConnection = isReConnection;
this.ReConnectionTime = reConnectionTime;
}
public void client_OnOpen(object sender, EventArgs e)
{
try
{
Console.WriteLine("Open!");
if (OnOpen != null)
OnOpen();
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
public void client_OnClose(object sender, CloseEventArgs e)
{
try
{
Console.WriteLine("Close!");
if (OnClose != null)
OnClose();
//掉线重连
if (IsReConnection)
{
Thread.Sleep(ReConnectionTime);
Start();
}
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
public void client_OnMessage(object sender, MessageEventArgs e)
{
try
{
Console.WriteLine("socket收到信息:" + e.Data);
if (OnMessage != null)
OnMessage(e.Data);
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
//else
// throw ex;
}
}
public void client_OnError(object sender, ErrorEventArgs e)
{
if (OnError != null)
OnError(e.Exception);
//记录日志
//掉线重连
if (IsReConnection)
{
Thread.Sleep(ReConnectionTime);
Start();
}
}
/// <summary>
/// 启动服务
/// </summary>
public void Start()
{
try
{
client.ConnectAsync();
}
catch (Exception ex)
{
//日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
/// <summary>
/// 关闭服务
/// </summary>
public void Close()
{
try
{
IsReConnection = false;
client.CloseAsync();
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
/// <summary>
/// 发送信息
/// </summary>
public void Send(string message)
{
try
{
client.Send(message);
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
/// <summary>
/// 发送信息
/// </summary>
public void Send(byte[] message)
{
try
{
client.Send(message);
}
catch (Exception ex)
{
//记录日志
if (OnError != null)
OnError(ex);
else
throw ex;
}
}
}
/// <summary>
/// WebSocket服务辅助类 作者韩永健
/// </summary>
public class WebSocketServerHelper
{
/// <summary>
/// 客户端信息
/// </summary>
public class ClientData
{
/// <summary>
/// IP
/// </summary>
public string IP { get; set; }
/// <summary>
/// 端口号
/// </summary>
public int Port { get; set; }
}
public delegate void ActionEventHandler(string ip, int port);
public delegate void MessageEventHandler(string ip, int port, string message);
public delegate void BinaryEventHandler(string ip, int port, byte[] message);
public delegate void ErrorEventHandler(string ip, int port, Exception ex);
/// <summary>
/// 客户端打开连接时调用
/// </summary>
public event ActionEventHandler OnOpen;
/// <summary>
/// 客户端关闭连接时调用
/// </summary>
public event ActionEventHandler OnClose;
/// <summary>
/// 收到客户端信息时调用
/// </summary>
public event MessageEventHandler OnMessage;
/// <summary>
/// 收到客户端信息时调用
/// </summary>
public event BinaryEventHandler OnBinary;
/// <summary>
/// 执行错误时调用
/// </summary>
public event ErrorEventHandler OnError;
/// <summary>
/// 服务
/// </summary>
private Fleck.WebSocketServer server;
/// <summary>
/// socket列表
/// </summary>
private Dictionary<string, IWebSocketConnection> socketList = new Dictionary<string, IWebSocketConnection>();
private List<ClientData> clientList = new List<ClientData>();
/// <summary>
/// 客户端ip列表
/// </summary>
public List<ClientData> ClientList
{
get
{
return clientList;
}
}
/// <summary>
/// 服务URL
/// </summary>
public string ServerUtl { private set; get; }
public WebSocketServerHelper(string serverUtl)
{
this.ServerUtl = serverUtl;
server = new Fleck.WebSocketServer(this.ServerUtl);
}
/// <summary>
/// 启动服务
/// </summary>
public void Start()
{
server.Start(socket =>
{
try
{
socket.OnOpen = () => SocketOpen(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort);
socket.OnClose = () => SocketClose(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort);
socket.OnMessage = message => SocketMessage(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, message);
socket.OnBinary = message => SocketBinary(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, message);
socket.OnError = ex => SocketError(socket.ConnectionInfo.ClientIpAddress, socket.ConnectionInfo.ClientPort, ex);
socketList.Add(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort, socket);
ClientList.Add(new ClientData() { IP = socket.ConnectionInfo.ClientIpAddress, Port = socket.ConnectionInfo.ClientPort });
}
catch (Exception ex)
{
}
});
}
/// <summary>
/// Socket错误
/// </summary>
private void SocketError(string ip, int port, Exception ex)
{
Console.WriteLine("Error!" + ip + ":" + port + " " + ex);
socketList.Remove(ip + ":" + port);
ClientList.RemoveAll(m => m.IP == ip && m.Port == port);
if (OnError != null)
OnError(ip, port, ex);
//else
// throw ex;
}
/// <summary>
/// Socket打开连接
/// </summary>
private void SocketOpen(string ip, int port)
{
try
{
Console.WriteLine("Open!" + ip + ":" + port);
if (OnOpen != null)
OnOpen(ip, port);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// Socket关闭连接
/// </summary>
private void SocketClose(string ip, int port)
{
try
{
Console.WriteLine("Close!" + ip + ":" + port);
socketList.Remove(ip + ":" + port);
ClientList.RemoveAll(m => m.IP == ip && m.Port == port);
if (OnClose != null)
OnClose(ip, port);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// 接收到的信息
/// </summary>
private void SocketBinary(string ip, int port, byte[] message)
{
try
{
Console.WriteLine("socket收到信息:byte[]");
if (OnBinary != null)
OnBinary(ip, port, message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// 接收到的信息
/// </summary>
private void SocketMessage(string ip, int port, string message)
{
try
{
Console.WriteLine("socket收到信息:" + message + " " + ip + ":" + port);
if (OnMessage != null)
OnMessage(ip, port, message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// 发送信息(单客户端)
/// </summary>
public void Send(string ip, int port, string message)
{
try
{
socketList[ip + ":" + port].Send(message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// 发送信息(单客户端)
/// </summary>
public void Send(string ip, int port, byte[] message)
{
try
{
socketList[ip + ":" + port].Send(message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(ip, port, ex);
else
throw ex;
}
}
/// <summary>
/// 发送信息(所有客户端)
/// </summary>
public void SendAll(string message)
{
for (int i = ; i < socketList.Count; i++)
{
var item = socketList.ElementAt(i);
try
{
item.Value.Send(message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(item.Value.ConnectionInfo.ClientIpAddress, item.Value.ConnectionInfo.ClientPort, ex);
else
throw ex;
}
}
}
/// <summary>
/// 发送信息(所有客户端)
/// </summary>
public void SendAll(byte[] message)
{
for (int i = ; i < socketList.Count; i++)
{
var item = socketList.ElementAt(i);
try
{
item.Value.Send(message);
}
catch (Exception ex)
{
if (OnError != null)
OnError(item.Value.ConnectionInfo.ClientIpAddress, item.Value.ConnectionInfo.ClientPort, ex);
else
throw ex;
}
}
}
}
C# WebSocket的更多相关文章
- 漫扯:从polling到Websocket
Http被设计成了一个单向的通信的协议,即客户端发起一个request,然后服务器回应一个response.这让服务器很为恼火:我特么才是老大,我居然不能给小弟发消息... 轮询 老大发火了,小弟们自 ...
- 细说WebSocket - Node篇
在上一篇提高到了 web 通信的各种方式,包括 轮询.长连接 以及各种 HTML5 中提到的手段.本文将详细描述 WebSocket协议 在 web通讯 中的实现. 一.WebSocket 协议 1. ...
- java使用websocket,并且获取HttpSession,源码分析
转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6238826.html 一:本文使用范围 此文不仅仅局限于spring boot,普通的sprin ...
- WebSocket - ( 一.概述 )
说到 WebSocket,不得不提 HTML5,作为近年来Web技术领域最大的改进与变化,包含CSS3.离线与存储.多媒体.连接性( Connectivity )等一系列领域,而即将介绍的 WebSo ...
- php+websocket搭建简易聊天室实践
1.前言 公司游戏里面有个简单的聊天室,了解了之后才知道是node+websocket做的,想想php也来做个简单的聊天室.于是搜集各种资料看文档.找实例自己也写了个简单的聊天室. http连接分为短 ...
- Demo源码放送:打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!
随着HTML5 WebSocket技术的日益成熟与普及,我们可以借助WebSocket来更加方便地打通BS与CS -- 因为B/S中的WebSocket可以直接连接到C/S的服务端,并进行双向通信.如 ...
- Cowboy 开源 WebSocket 网络库
Cowboy.WebSockets 是一个托管在 GitHub 上的基于 .NET/C# 实现的开源 WebSocket 网络库,其完整的实现了 RFC 6455 (The WebSocket Pro ...
- 借助Nodejs探究WebSocket
文章导读: 一.概述-what's WebSocket? 二.运行在浏览器中的WebSocket客户端+使用ws模块搭建的简单服务器 三.Node中的WebSocket 四.socket.io 五.扩 ...
- 细说websocket - php篇
下面我画了一个图演示 client 和 server 之间建立 websocket 连接时握手部分,这个部分在 node 中可以十分轻松的完成,因为 node 提供的 net 模块已经对 socket ...
- webSocket and LKDBHelper的使用说明
socketket与lkdbhelper来处理数据 客户需求: 当我们有需要从自己的后台推送消息给我们的用户时,用户需要实时的接收到来自我们的推送消息.前提是没有使用第三方的推送框架,那么这个使用we ...
随机推荐
- 关于leal和mov
最近在学习leal的时候遇到了一点非常迷惑的地方,就是leal是用来取有效地址的,但是为什么它也可以实现赋值呢?偶然发现一个博客讲的不错,遂自己记录一下 一个这样的例子 leal 7(%edx,%ed ...
- linux-0.11 内核源码学习笔记一(嵌入式汇编语法及使用)
linux内核源码虽然是用C写的,不过其中有很多用嵌入式汇编直接操作底层硬件的“宏函数”,要想顺利的理解内核理论和具体实现逻辑,学会看嵌入式汇编是必修课,下面内容是学习过程中的笔记:当做回顾时的参考. ...
- call apply bind的区别
都是天生自带的内置方法(Function.prototype),所有的函数都可以调取这三个方法,改变this指向 call 语法:fn.call(context,para1......) 把fn方法执 ...
- 【技巧】easyUI datagrid在隐藏时加载,显示时无法加载出界面
注意在显示时调用再调用一次resize就可以显示出来 $("#"+datagridId).datagrid("resize");
- C++学习笔记:多态篇之虚析构函数
动态多态中存在的问题:可能会产生内存泄漏! 以下通过一个例子向大家说明问什么会产生内存泄漏: class Shape//形状类 { public: Shape(); virtual double ca ...
- NumberFormatException: Invalid int类型不匹配异常——使用SQL数据库查询语句select * from blacknumber order by _id desc limit ?,20;出现
异常:类型不匹配 05-06 08:12:38.151: E/AndroidRuntime(14904): java.lang.NumberFormatException: Invalid int: ...
- java判断一个字符串是否为空,isEmpty和isBlank的区别
转载于:https://blog.csdn.net/liusa825983081/article/details/78246792 实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 S ...
- 异常java.lang.NumberFormatException解决
原因一:超出了int类型的取值范围 项目中要把十六进制字符串转化为十进制, 用到了到了Integer.parseInt(str1.trim(), 16):这个是不是后抛出java.lang.Numbe ...
- FNMP
Table of Contents 平台 FNMP安装 FNMP配置 php配置 mysql配置 nginx配置 phpMyAdmin配置 平台 freeBSD 12.0 FNMP安装 php安装 v ...
- __nw_connection_get_connected_socket_block_invoke Connection has no connected handle 解决办法
1. Xcode menu -> Product -> Edit Scheme... 2. Environment Variables -> Add -> Name: &quo ...