Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - How to create a WebSocket server using SuperWebSocket

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.

EDIT: This is the thing which I want to achieve. enter image description here

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 .

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

SuperWebSocket

Tutorial for Echo example

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...