如何使用 SuperWebSocket 创建 WebSocket 服务器 [英] How to create a WebSocket server using SuperWebSocket

查看:30
本文介绍了如何使用 SuperWebSocket 创建 WebSocket 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要 WebSocket 通信的应用程序.我所需要的只是一个具有线程可能性的简单 WebSocketServer.我发现 SuperWebSocket 可以满足我的需求.但是,我对 C# 的不熟悉使我无法理解代码.谁能告诉我如何创建一个简单的服务器,它应该回显从浏览器/网页发送的消息.我将非常感谢提供一些好的方向的人||引导||代码.我无法从他们的示例代码中弄清楚用法.

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.

这是我想要实现的事情.

This is the thing which I want to achieve.

如果有人说一个确切的解决方案,我会采用那个.

If anybody says an exact solution, I will adopt that one.

罗巴"已经给出了直接的回答.这就是我如何使用它.

"Robar" already gave the direct answer . This is jsut How I used it .

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);

推荐答案

SuperWebSocket

Echo 示例的教程

炼金术

如果您对其他 C# WebSocket 服务器开放,您可以使用 Alchemy.服务器实现非常简单:

If you are open to other C# WebSocket server you could use Alchemy. The server implementation is quite straight forward:

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.

这篇关于如何使用 SuperWebSocket 创建 WebSocket 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆