HOWTO: Create a simple WebSocket server

Apr 22, 2012 at 9:35 PM
Edited Apr 22, 2012 at 9:36 PM

Unless I am not looking in the right places, it appears that this site is missing a simple example on how to create a simple server in C# which uses the SuperWebSocket:

  1. Start Visual Studio
  2. Create a new Windows Form Application project.
  3. Add a Reference to SuperWebSocket.dll, SuperSocket.SocketBase.dll, and SuperSocket.SocketEngine.dll.
  4. Add a button to the form.  Change its caption to "Start WS"
  5. Add a checkbox to the form.  Change its caption to "Use SSL"
  6. Add the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuperWebSocket;
using SuperSocket;
using SuperSocket.SocketBase;

namespace SuperWebSocketTest
    public partial class Form1 : Form
        public Form1()

        SuperWebSocket.WebSocketServer ws;

        private void button1_Click(object sender, EventArgs e)
            SuperSocket.SocketBase.Config.RootConfig r = new SuperSocket.SocketBase.Config.RootConfig();
            SuperSocket.SocketBase.Config.ServerConfig s = new SuperSocket.SocketBase.Config.ServerConfig();
            s.Name = "SuperWebSocket";
            s.ServiceName = "SuperWebSocket";
            s.Ip = "Any";
            s.Port = 8089;
            s.Mode = SocketMode.Async;

            if (checkBox1.Checked == true)
                s.Mode = SocketMode.Sync;
                s.Security = "tls";

                SuperSocket.SocketBase.Config.CertificateConfig cert = new SuperSocket.SocketBase.Config.CertificateConfig();

                cert.FilePath = @"C:\SSL\yourhostname.cer";
                cert.Password = "";
                cert.IsEnabled = true;

                s.Certificate = cert;

            SuperSocket.SocketEngine.SocketServerFactory f = new SuperSocket.SocketEngine.SocketServerFactory();

            if (ws != null)
                ws = null;

            ws = new WebSocketServer();
            ws.Setup(r, s, f);
            ws.NewSessionConnected += new SessionEventHandler<WebSocketSession>(ws_NewSessionConnected);
            ws.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(ws_NewMessageReceived);
            ws.NewDataReceived += new SessionEventHandler<WebSocketSession, byte[]>(ws_NewDataReceived);

        void ws_NewDataReceived(WebSocketSession session, byte[] e)

        void ws_NewMessageReceived(WebSocketSession session, string e)
            session.SendResponse("Message: " + e);

        void ws_NewSessionConnected(WebSocketSession session)
            session.SendResponse("Hello From C#!");

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            if (ws != null)
                ws = null;

 Run the application.  Click on the button to start the server.  It will start listening on port 8089, but you can change it to be what ever you want.  Check the checkbox and click on the button again to start the server in secure mode.  Read and scroll down to Generate Self-signed Certificate With Microsoft Makecert if you need to generate a test certificate. 

Apr 23, 2012 at 2:19 AM

You'd better take a look at this class in Sample project:

There are two start up method:

1) Start by configuration

2) Start up by porgramming

Apr 23, 2012 at 2:52 AM

@kerryjiang: Thanks for the reply.  I think my post will help people as well because I am the type of programmer that wants to look to see what is involved to get a program off the ground prior to downloading and/or installing anything on my computer first.  If my post helps at least one person, I feel like I have done some justice.

Apr 23, 2012 at 2:54 AM

Great! Thank you.

May 8, 2012 at 2:13 PM
Edited May 8, 2012 at 2:14 PM



I compiled your code, the application runs, but when I connect in browser, the application quits with the following error :


A first chance exception of type 'System.MissingMethodException' occurred in SuperSocket.SocketEngine.dllThe program '[6404] SuperWebSocketTest.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).


Any leads ?



( x64 system )

May 8, 2012 at 3:58 PM

@giberisk: Are you able to step through the program to see at what line the error occurs?  I don't know if running on a 64 bit machine has anything to do with it, but it may as I am running Windows XP 32 bit.

@kerryjiang: Any thoughts?

May 8, 2012 at 5:17 PM

@vbguyny: Not quite sure how to do that..I am using the dll's as reference. You think you can upload your project ( including resources & binaries ) to a file sharing system ?

I really don't know what the problem is here.... internet searches get me nowhere.

Many thanks

May 8, 2012 at 9:09 PM

Problem solved.

I managed to connect to node.js


May 23, 2012 at 4:43 AM

@vbguyny: Thanks for your example, this is exactly what i was looking for. 


Jun 4, 2012 at 3:22 AM

@vbguyny: Well done example... great for getting started.  Thanks!

@kerryjiang: Nice job on both the SuperSocket and SuperWebSocket projects. 

I've got a (Firefox 12) browser connected to a (localhost) SuperWebSocket server which is connected to local SQLite database.  It's a nice self-contained C# project/application to allow the browser access to a local high-performance database.  I needed a localhost solution and didn't want to load Apache or IIS.


Jun 4, 2012 at 3:37 AM

Yes, it can run as an independent windows service by SuperSocket.SocketService.exe.

Jun 15, 2013 at 11:38 AM
Hi @vbguyny
I am using latest version of SuperWebSocket (version 0.8), which I downloaded from SuperWebSocket site.

After some changes in your provided code above, I am able run Web Socket server based on ws protocol.
I have created a test web socket client project in Asp.Net MVC and I make connection call to server using syntax like:
It works fine in ws. But when I try to run it in secure mode using https.... after a while, Firefox gives me following message in Browser console:
     Firefox can't establish a connection to the server at wss://my-pc:8089/Test
My server side code for button1 click event is as following:
            SuperSocket.SocketBase.Config.RootConfig r = new SuperSocket.SocketBase.Config.RootConfig();

            SuperSocket.SocketBase.Config.ServerConfig s = new SuperSocket.SocketBase.Config.ServerConfig();
            s.Name = "SuperWebSocket";
            s.Ip = "Any";
            s.Port = 8089;
            s.Mode = SocketMode.Tcp;

            if (checkBox1.Checked == true)
                s.Mode = SocketMode.Tcp;
                s.Security = "tls";

                SuperSocket.SocketBase.Config.CertificateConfig cert = new SuperSocket.SocketBase.Config.CertificateConfig();

                string pathToBaseDir = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;

                cert.FilePath = System.IO.Path.Combine(pathToBaseDir, "localhost.pfx");
                cert.Password = "supersocket";
                s.Certificate = cert;

            SuperSocket.SocketEngine.SocketServerFactory f = new SuperSocket.SocketEngine.SocketServerFactory();

            if (ws != null)
                ws = null;

            ws = new WebSocketServer();
            ws.Setup(r, s, f);
            ws.NewSessionConnected += new SessionHandler<WebSocketSession>(ws_NewSessionConnected);
            ws.NewMessageReceived += new SessionHandler<WebSocketSession, string>(ws_NewMessageReceived);
            ws.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(ws_NewDataReceived);
Its not working for wss case.
Please guide me where I am getting wrong. Thanks
Jun 15, 2013 at 12:32 PM
Are you sure the websocket server was started successfully? You can check the log for it.

BTW, you should create a new test certificate with the CN=my-pc, because you host which is used for accessing the server is my-pc.
Jun 15, 2013 at 12:47 PM
Yes my websocket server has started successfully. I have checked.

But can you explain your answer in a bit detail.
I am a novice when it comes to certificates. I dont know how to create a certificate and where to deploy it.
Please put some light on it. Thanks.
Jun 15, 2013 at 1:10 PM
Could you paste the starting log?

For certificate, please refer to the link below:
Jun 15, 2013 at 1:49 PM
This is the log which gets generated in Event Viewer, whenever my web page tries to connect to web socket server either through wss://my-pc:8089/Test or wss://localhost:8089/Test
Event code: 4005 
Event message: Forms authentication failed for the request. Reason: The ticket supplied was invalid. 
Event time: 6/15/2013 5:31:08 PM 
Event time (UTC): 6/15/2013 12:31:08 PM 
Event ID: 30d6a4788f6346d4a20b699174e08efc 
Event sequence: 58 
Event occurrence: 25 
Event detail code: 50201 
Application information: 
    Application domain: /LM/W3SVC/1/ROOT/SuperWebSocketClient-1-130157722640211221 
    Trust level: Full 
    Application Virtual Path: /SuperWebSocketClient 
    Application Path: C:\Users\faisal\Documents\Visual Studio 2010\Projects\SuperWebSocketClient\SuperWebSocketClient\ 
    Machine name: MY-PC 
Process information: 
    Process ID: 3472 
    Process name: w3wp.exe 
    Account name: IIS APPPOOL\ASP.NET v4.0 
Request information: 
    Request URL: http://my-pc/SuperWebSocketClient 
    Request path: /SuperWebSocketClient 
    User host address: fe80::a028:d80a:e53:7c69/SuperWebSocketClient 
    Is authenticated: False 
    Authentication Type:  
    Thread account name: IIS APPPOOL\ASP.NET v4.0 
Name to authenticate:  
Custom event details: 
It gives me response when I use ws protocol but not with wss.
I don't know which other log you are asking about.
I have but executed code in VS debugger line by line for checking SSL startup till ws.Start()
It executes fully with no exceptions occurring.
Jun 15, 2013 at 2:01 PM
No, please check SuperWebSocket's log (Logs/xxx.log)
Jun 15, 2013 at 2:32 PM
@TopGun: The issue is most likely related to the fact that the Ssl certificate isn't a valid certificate from your browser's point of view. You need to import your ssl certificate into your browser's certificate store before trying to use wss://

You can Google on how to do this.
Jun 17, 2013 at 10:41 AM
Hi vbguyny,
I am novice in terms of SSL certificates. I have looked over google but failed to understand how they are generated and then used on client.
Can you plz guide me on this in simple words ?

I just want to access a secure web socket url like following:
Jun 17, 2013 at 10:45 AM
Did you find the log of SuperWebSocket? If you cannot find it, please check the doc I mentioned previously to enable it.
Please ensure your server instance was started successfully.
Jun 17, 2013 at 10:48 AM
If you have confirmed the server was started without problem, then make sure your certificate you are using is correct.
The tool which can make the certificate you need is mentioned in the doc:
Nov 1, 2013 at 7:18 AM
can u provide me the sample project or dll use in this project because i am using that but it generate error of missing assembly and namespace
Nov 1, 2013 at 7:50 AM
All samples are in the source code folder!
Nov 1, 2013 at 9:28 AM
Thank U,
Sir i want to create one windows application which play the role of server and web site that play the role of client. I am using ur code from "HOWTO: Create a simple WebSocket server" and also give reference of all dll that u already told but it generate the error of missing assembly.
I am devolop the code on vs2008 in 3.5 net framework so please give me the solution as soon as possible.
Thank in advanced......
Nov 1, 2013 at 9:39 AM
1) If you meet any error, tell us the error details, then we can figure it out
2) The official sample projects are in the source code
Nov 1, 2013 at 9:54 AM
Edited Nov 1, 2013 at 9:55 AM
Sir this error which i encounterd:

"The type 'System.Configuration.ConfigurationElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."

for this line of code:
SuperSocket.SocketBase.Config.ServerConfig s = new SuperSocket.SocketBase.Config.ServerConfig();

Can u give me the path from which i can download the dll.
Nov 1, 2013 at 9:57 AM

The error has told you how to fix it. Ask the colleague who sit beside you and can program in C#, he/she must can help you.
Nov 1, 2013 at 10:01 AM
Edited Nov 1, 2013 at 10:02 AM
Can u give me the path from which i can download the dll.
Error occured because of dll
Nov 1, 2013 at 10:15 AM
Edited Nov 1, 2013 at 10:18 AM
give me the dll which u used in ur source code
Nov 1, 2013 at 10:37 AM
Add Referece => Find the missing reference in the ".NET" tab.
The "System.Configuration" is not a third party assembly, it is included in .NET BCL.

You can reference it from GAC directly.
Nov 9, 2013 at 7:31 AM
Yes sir i got it!!!
But all dll work for only .net framework 3.5
Not for .net framework 3.5 client platform or 4.0
Thank you for reply
Nov 9, 2013 at 5:03 PM
@Ganeshkadam76: That is a good point. You need to change your project settings in .NET to change to .NET Framework 3.5 (Not client platform). That one gets me every time!
Dec 19, 2013 at 6:08 AM
HI Sir,

I am using ur sample code and done websocket programming but i want server side code in
Dec 19, 2013 at 12:08 PM
You can host your server code in a web project. The sample LiveWebChat goes that way.