C# 如何使用 SuperWebSocket 创建 WebSocket 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9696594/
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
How to create a WebSocket server using SuperWebSocket
提问by prabhakaran
I am creating an application which needs WebSocket Communication. All I need is a simple WebSocketServer with threading possibilities. I found that SuperWebSocket can satisfy my needs. But, my poor familiarity with C# makes trouble in understanding the code. Can anybody show me How to create a simple server Which should echo the message which is sent from the browser/WebPage. I will be very thankful to the person who shows some good direction||guide||code. I couldn't figure out the usage from their sample codes.
我正在创建一个需要 WebSocket 通信的应用程序。我所需要的只是一个具有线程可能性的简单 WebSocketServer。我发现 SuperWebSocket 可以满足我的需求。但是,我对 C# 的不熟悉使我无法理解代码。谁能告诉我如何创建一个简单的服务器,它应该回显从浏览器/网页发送的消息。我将非常感谢提供一些好的方向的人||引导||代码。我无法从他们的示例代码中弄清楚用法。
EDIT:This is the thing which I want to achieve.

编辑:这是我想要实现的目标。

If anybody says an exact solution, I will adopt that one.
如果有人说一个确切的解决方案,我会采用那个。
EDIT:"Robar" already gave the direct answer . This is jsut How I used it .
编辑:“Robar”已经给出了直接的答案。这就是我如何使用它。
this.NewSessionConnected += new SessionEventHandler<WebSocketSession>(this.WebSocketServer_NewSessionConnected);
this.NewDataReceived += new SessionEventHandler<WebSocketSession, byte[]>(this.WebSocketServer_NewDataReceived);
this.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(this.WebSocketServer_NewMessageReceived);
this.SessionClosed += new SessionEventHandler<WebSocketSession, SuperSocket.SocketBase.CloseReason>(this.WebSocketServer_SessionClosed);
回答by tuoxie007
class Program
{
static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Loopback, 8181);
listener.Start();
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
writer.WriteLine("Upgrade: WebSocket");
writer.WriteLine("Connection: Upgrade");
writer.WriteLine("WebSocket-Origin: http://localhost:8080");
writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
writer.WriteLine("");
}
listener.Stop();
}
}
回答by Robar
SuperWebSocket
超级网络套接字
Tutorial for Echo example
Echo 示例教程
Alchemy
炼金术
If you are open to other C# WebSocket server you could use Alchemy. The server implementation is quite straight forward:
如果您对其他 C# WebSocket 服务器开放,则可以使用Alchemy。服务器实现非常简单:
static void Main(string[] args) {
var aServer = new WSServer(8100, IPAddress.Any) {
DefaultOnReceive = new OnEventDelegate(OnReceive),
DefaultOnSend = new OnEventDelegate(OnSend),
DefaultOnConnect = new OnEventDelegate(OnConnect),
DefaultOnConnected = new OnEventDelegate(OnConnected),
DefaultOnDisconnect = new OnEventDelegate(OnDisconnect),
TimeOut = new TimeSpan(0, 5, 0)
};
aServer.Start();
}
static void OnConnected(UserContext aContext) {
Console.WriteLine("Client Connection From : " + aContext.ClientAddress.ToString());
// TODO: send data back
}
As mentioned on their website, they have a simple chat example.
正如在他们的网站上提到的,他们有一个简单的聊天示例。

