Back to the full WebSockets listWhat are WebSockets?

NetGain

GitHubStarsLast commitProject createdClosed vsOpen issues
918
4 years ago
8 years ago
5 / 14

Welcome to StackExchange.NetGain!

Warning: reduced maintenance

A few things have changed since this library was written:

As of late 2018, Stack Overflow (Stack Exchange) have migrated away from NetGain, instead using Kestrel's inbuilt web-socket support over HTTP/1.*; it is entirely possible that as we delve more into HTTP/2, we look more in the direction of SSE (EventSource).

So: we're not actively working with, or working on, this library. If you have a burning desire to take it further as an official maintainer (perhaps with some tweaks to naming, obviously) - let me know!


NetGain supports:

Build

StackExchange.NetGain is built as a single assembly, StackExchange.NetGain.dll.

StackExchange.NetGain is available on the [NuGet Gallery]

You can add StackExchange.NetGain to your project with the NuGet Package Manager, by using the following command in the Package Manager Console.

PM> Install-Package StackExchange.NetGain

WebSocket Server Example

using System;
using System.Net;
using StackExchange.NetGain;
using StackExchange.NetGain.WebSockets;

namespace Example
{
  public class Program
  {
    public static void Main (string[] args)
    {
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
        using(var server = new TcpServer())
        {
            server.ProtocolFactory = WebSocketsSelectorProcessor.Default;
            server.ConnectionTimeoutSeconds = 60;
            server.Received += msg =>
            {
                var conn = (WebSocketConnection)msg.Connection;
                string reply = (string)msg.Value + " / " + conn.Host;
                Console.WriteLine("[server] {0}", msg.Value);
                msg.Connection.Send(msg.Context, reply);
            };
            server.Start("abc", endpoint);
            Console.WriteLine("Server running");

            Console.ReadKey();
        }
        Console.WriteLine("Server dead; press any key");
        Console.ReadKey();
      }
    }
  }
}