.NET 용 WebSocket 클라이언트가 구현되어 있습니까? [닫은
문제
Windows 양식 또는 WPF 응용 프로그램에서 WebSockets를 사용하고 싶습니다. 아직 구현 된 WebSockets를 지원하는 .NET-Control이 있습니까? 아니면 오픈 소스 프로젝트가 시작 되었습니까?
WebSockets를 지원하는 Java 클라이언트를위한 오픈 소스 솔루션도 도움이 될 수 있습니다.
해결책
이제 SuperWebSocket에는 WebSocket 클라이언트 구현도 포함되어 있습니다SuperWebSocket 프로젝트 홈페이지
기타 .NET 클라이언트 구현에는 다음이 포함됩니다.
다른 팁
다음은 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);
}
}
Websockets에 대한 지원은입니다 .NET 4.5에서 나옵니다. 해당 링크에는 사용 예가 포함되어 있습니다 System.net.websockets.websocket 수업.
Kaazing.com은 WebSocket에 액세스 할 수있는 .NET 클라이언트 라이브러리를 제공합니다. 그들은 온라인으로 튜토리얼이 있습니다 점검 목록 : Microsoft .NET JMS 클라이언트를 구축하십시오 그리고 체크리스트 : Microsoft .NET AMQP 클라이언트를 구축하십시오
이있다 Java WebSocket 클라이언트 Github에 프로젝트.
클라이언트 구현이 있습니다. http://websocket4net.codeplex.com/!
another choice: xsockets.net, 서버와 클라이언트를 구현했습니다.
서버를 설치할 수 있습니다.
PM> Install-Package XSockets
또는 클라이언트를 설치합니다.
PM> Install-Package XSockets.Client
현재 버전은 다음과 같습니다. 3.0.4
매우 간단한 프로토콜입니다. Java 구현이 있습니다 여기 C#으로 변환하기에는 너무 어렵지 않아야합니다. 내가 그 일을하면 여기에 게시하겠습니다 ...
최근 상호 운용성 브리지 및 랩스 센터 (Labs Center)는 Websockets 프로토콜 사양의 두 초안의 프로토 타입 구현 (관리 코드)을 발표했습니다.
Draft-Hixie-ThewebsocketProtocol-75 그리고 Draft-Hixie-ThewebsocketProtocol-76
프로토 타입은 HTML5 Labs에서 찾을 수 있습니다. 나는 들어갔다 이 블로그 게시물 내가 찾은 모든 정보 (지금까지) WCF를 사용하여 수행 할 수있는 방법에 대한 코드 스 니펫.
조금 더 가벼운 것을 원한다면 친구와 내가 발표 한 C# 서버를 확인하십시오. https://github.com/olivine-labs/alchemy-websockets
플래시 웹 소켓뿐만 아니라 바닐라 웹 소켓을 지원합니다. 확장 성과 효율성에 중점을 둔 온라인 게임을 위해 구축되었습니다.
연금술도 있습니다. http://olivinelabs.com/alchemy-websockets/ 다소 멋지다.