11

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); 
2
  • Just wanted to state that superwebsocket is already multithreads. A session is independent from another session. Commented Oct 21, 2012 at 14:37
  • Is there any way of using SSL -> tsl1.2 or tsl1.3 with SuperWebSocket? Commented Aug 20, 2020 at 12:53

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Alchemy is really cool! unfortunally it lack some features right now. SuperWebSockets is probably a better option for now.
@Rushino: that's right. SuperWebSockets is currently the perfect solution for the projects in our company.
That nice to hear ! i just started using it and i think its really wonderful how it work and it work very well so far. So if you using it in your company that prove my point.
This project doesn't seem to be supported any more. There are several major issues and pull requests open for a year+.
@BryanAnderson yeah thats right, Alchemy seems to be unsupported. As of today I would recommend SignalR for WebSocket communication.
1
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(); } } 

4 Comments

Err, that's an incomplete handshake with no later echo of client requests. Do you have more code to post?
@tuoxie Your solution worked well. But,It didnt work when I removed that "using" and declared the "writer" as as object. Can I know Why?
@simonc Te remaining part is computing the sha1-base64 string and sending it to the client.
I found the answer for my comment. I have to use "writer.Flush()", if I drop the using.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.