Html 是否有为 .NET 实现的 WebSocket 客户端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2064641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a WebSocket client implemented for .NET?
提问by Jonas
I would like to use WebSockets in my Windows Forms or WPF-application. Is there a .NET-control that is supporting WebSockets implemented yet? Or is there any open source project started about it?
我想在我的 Windows 窗体或 WPF 应用程序中使用 WebSockets。是否有支持已实现的 WebSockets 的 .NET 控件?或者有没有关于它的开源项目?
An open source solution for a Java Client supporting WebSockets could also help me.
支持 WebSockets 的 Java 客户端的开源解决方案也可以帮助我。
采纳答案by Kerry Jiang
Now, SuperWebSocket also includes a WebSocket client implementation SuperWebSocket Project Homepage
现在,SuperWebSocket 还包括一个 WebSocket 客户端实现 SuperWebSocket 项目主页
Other .NET client implementations include:
其他 .NET 客户端实现包括:
回答by jhurliman
Here's a quick first pass at porting that Java code to C#. Doesn't support SSL mode and has only been very lightly tested, but it's a start.
这是将 Java 代码移植到 C# 的快速入门。不支持 SSL 模式并且只经过了非常简单的测试,但这是一个开始。
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class WebSocket
{
private Uri mUrl;
private TcpClient mClient;
private NetworkStream mStream;
private bool mHandshakeComplete;
private Dictionary<string, string> mHeaders;
public WebSocket(Uri url)
{
mUrl = url;
string protocol = mUrl.Scheme;
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
throw new ArgumentException("Unsupported protocol: " + protocol);
}
public void SetHeaders(Dictionary<string, string> headers)
{
mHeaders = headers;
}
public void Connect()
{
string host = mUrl.DnsSafeHost;
string path = mUrl.PathAndQuery;
string origin = "http://" + host;
mClient = CreateSocket(mUrl);
mStream = mClient.GetStream();
int port = ((IPEndPoint)mClient.Client.RemoteEndPoint).Port;
if (port != 80)
host = host + ":" + port;
StringBuilder extraHeaders = new StringBuilder();
if (mHeaders != null)
{
foreach (KeyValuePair<string, string> header in mHeaders)
extraHeaders.Append(header.Key + ": " + header.Value + "\r\n");
}
string request = "GET " + path + " HTTP/1.1\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"Host: " + host + "\r\n" +
"Origin: " + origin + "\r\n" +
extraHeaders.ToString() + "\r\n";
byte[] sendBuffer = Encoding.UTF8.GetBytes(request);
mStream.Write(sendBuffer, 0, sendBuffer.Length);
StreamReader reader = new StreamReader(mStream);
{
string header = reader.ReadLine();
if (!header.Equals("HTTP/1.1 101 Web Socket Protocol Handshake"))
throw new IOException("Invalid handshake response");
header = reader.ReadLine();
if (!header.Equals("Upgrade: WebSocket"))
throw new IOException("Invalid handshake response");
header = reader.ReadLine();
if (!header.Equals("Connection: Upgrade"))
throw new IOException("Invalid handshake response");
}
mHandshakeComplete = true;
}
public void Send(string str)
{
if (!mHandshakeComplete)
throw new InvalidOperationException("Handshake not complete");
byte[] sendBuffer = Encoding.UTF8.GetBytes(str);
mStream.WriteByte(0x00);
mStream.Write(sendBuffer, 0, sendBuffer.Length);
mStream.WriteByte(0xff);
mStream.Flush();
}
public string Recv()
{
if (!mHandshakeComplete)
throw new InvalidOperationException("Handshake not complete");
StringBuilder recvBuffer = new StringBuilder();
BinaryReader reader = new BinaryReader(mStream);
byte b = reader.ReadByte();
if ((b & 0x80) == 0x80)
{
// Skip data frame
int len = 0;
do
{
b = (byte)(reader.ReadByte() & 0x7f);
len += b * 128;
} while ((b & 0x80) != 0x80);
for (int i = 0; i < len; i++)
reader.ReadByte();
}
while (true)
{
b = reader.ReadByte();
if (b == 0xff)
break;
recvBuffer.Append(b);
}
return recvBuffer.ToString();
}
public void Close()
{
mStream.Dispose();
mClient.Close();
mStream = null;
mClient = null;
}
private static TcpClient CreateSocket(Uri url)
{
string scheme = url.Scheme;
string host = url.DnsSafeHost;
int port = url.Port;
if (port <= 0)
{
if (scheme.Equals("wss"))
port = 443;
else if (scheme.Equals("ws"))
port = 80;
else
throw new ArgumentException("Unsupported scheme");
}
if (scheme.Equals("wss"))
throw new NotImplementedException("SSL support not implemented yet");
else
return new TcpClient(host, port);
}
}
回答by Stephen Rudolph
Support for WebSockets is coming in .NET 4.5. That links also contains an example using the System.Net.WebSockets.WebSocketclass.
.NET 4.5 将支持 WebSockets 。该链接还包含一个使用System.Net.WebSockets.WebSocket类的示例。
回答by Robert Christie
Kaazing.com provide a .NET client library that can access websockets. They have tutorials online at Checklist: Build Microsoft .NET JMS Clientsand Checklist: Build Microsoft .NET AMQP Clients
Kaazing.com 提供了一个可以访问 websockets 的 .NET 客户端库。他们在Checklist: Build Microsoft .NET JMS Clients和Checklist: Build Microsoft .NET AMQP Clients 上有在线教程
There is a Java Websocket Clientproject on github.
github 上有一个Java Websocket Client项目。
回答by Kerry Jiang
There is a client implementation: http://websocket4net.codeplex.com/!
有一个客户端实现:http: //websocket4net.codeplex.com/!
回答by sendreams
another choice: XSockets.Net, has implement server and client.
另一种选择:XSockets.Net,有实现服务器和客户端。
can install server by:
可以通过以下方式安装服务器:
PM> Install-Package XSockets
or install client by:
或通过以下方式安装客户端:
PM> Install-Package XSockets.Client
current version is: 3.0.4
当前版本是: 3.0.4
回答by shivakumar
Here is the list of .net supported websocket nuget packages
这是 .net 支持的 websocket nuget 包的列表
I prefer following clients
我更喜欢关注客户
回答by billywhizz
回答by Nikos Baxevanis
Recently the Interoperability Bridges and Labs Center released a prototype implementation (in managed code) of two drafts of the WebSockets protocol specification:
最近,互操作性桥梁和实验室中心发布了 WebSockets 协议规范的两个草案的原型实现(在托管代码中):
draft-hixie-thewebsocketprotocol-75and draft-hixie-thewebsocketprotocol-76
Draft-hixie-thewebsocketprotocol-75和Draft-hixie-thewebsocketprotocol-76
The prototype can be found at HTML5 Labs. I put in this blog postall the information I found (until now) and snippets of code about how this can be done using WCF.
原型可以在 HTML5 Labs 找到。我在这篇博文中列出了我找到的所有信息(直到现在)以及关于如何使用 WCF 完成此操作的代码片段。
回答by Hyman Lawson
If you'd like something a little more lightweight, check out a C# server that a friend and I released: https://github.com/Olivine-Labs/Alchemy-Websockets
如果您想要更轻量级的东西,请查看我和朋友发布的 C# 服务器:https: //github.com/Olivine-Labs/Alchemy-Websockets
Supports vanilla websockets as well as flash websockets. It was built for our online game with a focus on scalability and efficiency.
支持 vanilla websockets 以及 flash websockets。它是为我们的在线游戏而构建的,重点是可扩展性和效率。