• Stars
    star
    273
  • Rank 150,780 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple C# async websocket server and client for reliable transmission and receipt of data

alt tag

Watson Websocket

NuGet Version NuGet

WatsonWebsocket is the EASIEST and FASTEST way to build client and server applications that rely on messaging using websockets. It's. Really. Easy.

Thanks and Appreciation

Many thanks and much appreciation to those that take the time to make this library better!

@BryanCrotaz @FodderMK @caozero @Danatobob @Data33 @AK5nowman @jjxtra @MartyIX @rajeshdua123 @tersers @MacKey-255 @KRookoo1 @joreg @ilsnk @xbarra @mawkish00 @jlopvet @marco-manfroni-perugiatiming @GiaNTizmO @exergist @ebarale99 @WarstekHUN @Rubidium37 @codengine

Test App

A test project for both client (TestClient) and server (TestServer) are included which will help you understand and exercise the class library.

A test project that spawns a server and client and exchanges messages can be found here: https://github.com/jchristn/watsonwebsockettest

Supported Operating Systems

WatsonWebsocket currently relies on websocket support being present in the underlying operating system. Windows 7 does not support websockets.

SSL

SSL is supported in WatsonWebsocket. The constructors for WatsonWsServer and WatsonWsClient accept a bool indicating whether or not SSL should be enabled. Since websockets, and as a byproduct WatsonWebsocket, use HTTPS, they rely on certificates within the certificate store of your operating system. A test certificate is provided in both the TestClient and TestServer projects which can be used for testing purposes. These should NOT be used in production.

For more information on using SSL certificates, please refer to the wiki.

New in v4.0.x

  • Breaking changes
  • Clients now identified by Guid in ClientMetadata
  • ListClients now returns full ClientMetadata
  • Send* methods now take guid as opposed to IpPort
  • Add targeting for .NET 7.0 and .NET Framework 4.8
  • Fix for Blazor WASM, thank you @ebarale99
  • Fix for invalid control characters, thank you @WarstekHUN

Server Example

using WatsonWebsocket;

WatsonWsServer server = new WatsonWsServer("[ip]", port, true|false);
server.ClientConnected += ClientConnected;
server.ClientDisconnected += ClientDisconnected;
server.MessageReceived += MessageReceived; 
server.Start();

static void ClientConnected(object sender, ConnectionEventArgs args) 
{
    Console.WriteLine("Client connected: " + args.Client.ToString());
}

static void ClientDisconnected(object sender, DisconnectionEventArgs args) 
{
    Console.WriteLine("Client disconnected: " + args.Client.ToString());
}

static void MessageReceived(object sender, MessageReceivedEventArgs args) 
{ 
    Console.WriteLine("Message received from " + args.Client.ToString() + ": " + Encoding.UTF8.GetString(args.Data));
}

Client Example

using WatsonWebsocket;

WatsonWsClient client = new WatsonWsClient("[server ip]", [server port], true|false);
client.ServerConnected += ServerConnected;
client.ServerDisconnected += ServerDisconnected;
client.MessageReceived += MessageReceived; 
client.Start(); 

static void MessageReceived(object sender, MessageReceivedEventArgs args) 
{
    Console.WriteLine("Message from server: " + Encoding.UTF8.GetString(args.Data));
}

static void ServerConnected(object sender, EventArgs args) 
{
    Console.WriteLine("Server connected");
}

static void ServerDisconnected(object sender, EventArgs args) 
{
    Console.WriteLine("Server disconnected");
}

Client Example using Browser

server = new WatsonWsServer("http://localhost:9000/");
server.Start();
let socket = new WebSocket("ws://localhost:9000/test/");
socket.onopen = function () { console.log("success"); };
socket.onmessage = function (msg) { console.log(msg.data); };
socket.onclose = function () { console.log("closed"); };
// wait a moment
socket.send("Hello, world!");

Accessing from Outside Localhost

When you configure WatsonWebsocket to listen on 127.0.0.1 or localhost, it will only respond to requests received from within the local machine.

To configure access from other nodes outside of localhost, use the following:

  • Specify the exact DNS hostname upon which WatsonWebsocket should listen in the Server constructor. The HOST header on incoming HTTP requests MUST match this value (this is an operating system limitation)
  • If you want to listen on more than one hostname or IP address, use * or +. You MUST:
    • Run WatsonWebsocket as administrator for this to work (this is an operating system limitation)
    • Use the server constructor that takes distinct hostname and port values (not the URI-based constructor)
  • If you want to use a port number less than 1024, you MUST run WatsonWebsocket as administrator (this is an operating system limitation)
  • If you listen on an interface IP address other than 127.0.0.1, you MAY need to run as administrator (this is operating system dependent)
  • Open a port on your firewall to permit traffic on the TCP port upon which WatsonWebsocket is listening
  • You may have to add URL ACLs, i.e. URL bindings, within the operating system using the netsh command:
    • Check for existing bindings using netsh http show urlacl
    • Add a binding using netsh http add urlacl url=http://[hostname]:[port]/ user=everyone listen=yes
    • Where hostname and port are the values you are using in the constructor
  • If you are using SSL, you will need to install the certificate in the certificate store and retrieve the thumbprint
  • If you're still having problems, please do not hesitate to file an issue here, and I will do my best to help and update the documentation.

Version History

Please refer to CHANGELOG.md for details.

More Repositories

1

SuperSimpleTcp

Simple wrapper for TCP client and server in C# with SSL support
C#
445
star
2

DatabaseWrapper

Simple database wrapper for Microsoft SQL Server, MySQL, PostgreSQL, and Sqlite written in C# supporting dynamic query building and nesting using expressions.
C#
95
star
3

HttpServerLite

TCP-based user-space HTTP and HTTPS server, written in C#, with no dependency on http.sys.
C#
72
star
4

Blobject

BlobHelper is a common, consistent storage interface for Microsoft Azure, Amazon S3 (and S3-compliant storage), and local filesystem written in C#.
C#
59
star
5

PuppyProxy

A simple HTTP proxy in C# including support for HTTP CONNECT tunneling
C#
58
star
6

CavemanTcp

CavemanTcp is a simple TCP client and server providing callers with easy integration and full control over network reads and writes.
C#
57
star
7

Kvpbase

Scalable, simple RESTful object storage platform, written in C#
C#
52
star
8

Less3

Less3 is an S3-compatible object storage server that runs on your laptop, servers, just about anywhere!
C#
50
star
9

RestDb

RESTful HTTP/HTTPS server for Microsoft SQL Server, MySQL, and PostgreSQL
C#
44
star
10

SimpleUdp

SimpleUdp is a super simple way of building UDP clients and servers in C#.
C#
39
star
11

WatsonDedupe

Self-contained C# library for data deduplication using Sqlite
C#
35
star
12

IndexEngine

IndexEngine is a lightweight document and text indexing platform written in C#.
C#
35
star
13

Gatekeeper

Lightweight library in C# for implementing roles-based access control (RBAC). With Gatekeeper, you can define users, roles, resources, and permissions, and authorize requests.
C#
35
star
14

LoggingModule

Brain-dead easy C# class library for logging to syslog, console, and file
C#
30
star
15

Caching

Simple FIFO and LRU cache in C#
C#
29
star
16

SqliteHelper

Simple wrapper for Sqlite databases written in C#.
C#
27
star
17

S3Server

Emulated Amazon Web Services (AWS) Simple Storage Service (S3) server-side interface.
C#
26
star
18

SyslogServer

C# Syslog Server
C#
26
star
19

RestWrapper

RestWrapper is a simple C# class library to help simplify sending REST API requests and retrieving responses (RESTful HTTP and HTTPS)
C#
26
star
20

WatsonMesh

A simple mesh networking library in C# based on WatsonTcp
C#
24
star
21

WatsonORM

WatsonORM is a lightweight and easy to use object-relational mapper (ORM) in C# for .NET Core.
C#
22
star
22

LiteGraph

Lightweight graph database built using Sqlite
C#
21
star
23

WatsonCluster

A simple C# class using Watson TCP to enable a one-to-one high availability cluster.
C#
20
star
24

ContainerFS

Single-user self-contained file system written in C#
C#
16
star
25

IpMatcher

C# library for maintaining a match list of IP addresses and networks and comparing inputs to see if a match exists.
C#
15
star
26

RegexMatcher

Regex Matching Library in C#
C#
12
star
27

komodo

Komodo: C# Information storage, metadata, search, and retrieval platform
C#
11
star
28

PQueue

Lightweight, persistent, thread-safe, disk-based queue written in C#
C#
11
star
29

StringSimilarity

Simple US-English String Similarity Score Calculator
C#
10
star
30

NetLedger

NetLedger is a simple, self-contained ledgering library for adding debits and credits, checking balances, and performing commits on pending entries.
C#
10
star
31

SimpleServer

Simple C# RESTful web server using Watson Webserver.
C#
8
star
32

EmailWrapper

Simple email wrapper in C# for SMTP and Mailgun
C#
8
star
33

ClosestString

Simple static library to identify the closest string from a list, and its edit distance, based on a supplied input string.
C#
7
star
34

TaskHandler

A simple C# class library to help manage running a queue of tasks without relinquishing control.
C#
7
star
35

uscale

uscale - HTTP/HTTPS Loadbalancer in C#
C#
6
star
36

PythonSharp

Console wrapper for running Python scripts in C#
C#
6
star
37

UrlMatcher

Simple URL matcher library allowing you to match based on explicit string or parameters
C#
5
star
38

KeyGenerator

Simple program and class library to generate DNA codewords to be used as keys given a series of constraints.
C#
5
star
39

AWSSignatureGenerator

A simple class library for generating AWS V4 signatures.
C#
5
star
40

PdfTextExtractor

A simple C# shell wrapper for the wonderful pdfplumber library in Python to extract text from .PDF files
C#
4
star
41

DocumentTextExtractor

Simple C# library for extracting text and metadata from .docx, .pptx, and .xlsx files
C#
4
star
42

S3Lite

Lightweight Amazon S3 client without all the heft and dependency drag of the official library.
C#
4
star
43

SerializationHelper

SerializationHelper is a simple wrapper around System.Text.Json to overcome some of the common challenges developers face when using Microsoft's JSON library compared to the Newtonsoft.Json library.
C#
4
star
44

TcpTest

Test project to better handle TCP disconnects and failures in SimpleTcp, WatsonTcp, and BigQ.
C#
4
star
45

OpenAuditLog

Simple C# event library providing event persistence and allowing you to write your own emitters
C#
4
star
46

PythonLoader

Example using pythonnet to run Python from C# with or without virtual environments in Windows, Mac, or Linux
C#
4
star
47

SlidingWindow

SlidingWindow provides an interface to retrieve chunks from a byte array using a sliding window.
C#
4
star
48

Webhooky

Webhooky helps you add webhooks to your app, with simple classes and methods for managing rules, targets, events, retries, and status.
C#
4
star
49

PrettyId

Simple static method for generating IDs like those used by Stripe and other services
C#
3
star
50

RosettaStone

Backend server for DNA data storage Rosetta Stone initiative
C#
3
star
51

SendWithBrevo

A simple C# class library to help simplify sending emails using Brevo.
C#
3
star
52

GoogleMapsClient

I needed a simple way to parse addresses and resolve coordinates to an address. Plug in a Google Maps API key and you're all set.
C#
3
star
53

PerformanceStatistics

Library for capturing performance statistics from commonly-used performance counters. Currently only supports Windows.
C#
3
star
54

PDictionary

PDictionary is a persistent dictionary that stores dictionary contents to a JSON file.
C#
3
star
55

Timestamps

Simple class containing start time, end time, and total time.
C#
3
star
56

ArangoDBLite

Lightweight ArangoDB SDK written in C#
C#
3
star
57

KvpbaseCSharpSDK

C# SDK for kvpbase
C#
3
star
58

SendWithMailgun

Simple class library to send email using Mailgun.
C#
3
star
59

HtmlPageBuilder

Simple library that allows you to build HTML strings including head and body components
C#
3
star
60

MusicLabeler

Just something simple to apply ID3 tags to MP3 files based on the filename structure.
C#
2
star
61

ChunkDecoder

Library for decoding chunk-transfer-encoded data, particularly from HTTP.
C#
2
star
62

Shelli

Shelli is a simple static class to run things in your shell, tested on both Windows and Ubuntu.
C#
2
star
63

WatsonFunction

Function as a service (FaaS) platform for .NET core functions written in C#
C#
2
star
64

NuGetPackTest

Just testing how to pack dependencies inside NuGet packages
C#
2
star
65

ExpressionTree

A simple library for creating an expression tree.
C#
2
star
66

HttpContextLite

Nothing to see here
C#
2
star
67

WatsonWebsocketJs

Javascript client for Watson Websocket
JavaScript
2
star
68

CommonPrefixes

Evalutes a list of strings and returns a list representing common prefixes found within based on a given delimiter.
C#
2
star
69

SerializableDataTable

SerializableDataTable is a library providing an abstraction class that allows you to serialize and deserialize data to and from a DataTable instance.
C#
2
star
70

Keyval

1
star
71

NeverBounce

Simple library for validating email addresses using NeverBounce.
C#
1
star
72

WatsonDedupeUI

UI for WatsonDedupe library
C#
1
star
73

StripeWrapper

A simple C# library for Stripe charges and refunds
C#
1
star
74

EditDistance

A simple library to assist in understanding edit distance algorithms.
C#
1
star
75

Geocode

Simple Geocode Implementation using Google Maps API
C#
1
star
76

Inputty

Inputty is a simple library that helps simplify the process of getting input from the console.
C#
1
star
77

WatsonTcpTest

Simple test project for WatsonTcp.
C#
1
star
78

KeepaliveTest

Test project to test TCP keepalives
C#
1
star
79

PaigoDotNet

Simple C# library for using Paigo
C#
1
star
80

WatsonWebserverTest

C#
1
star
81

WatsonWebsocketTest

Simple test wrapper for WatsonWebsocket
C#
1
star
82

WatsonWindowsService

C#
1
star