GithubHelp home page GithubHelp logo

codefork / netsocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from valudio/netsocket

0.0 2.0 0.0 89 KB

Small library to easily deal with websockets using .NET Standard or .NET 4.6.2

License: MIT License

C# 92.45% HTML 7.55%

netsocket's Introduction

netsocket

Small library to easily deal with websockets using .NET Standard 1.5 or .NET 4.6.2

How can I consume this?

For the .NET 4.6.2 version you can use the Nuget Package:

# .NET Core
PM> Install-Package Valudio.NetSocket.NET 
# .NET Standard
PM> Install-Package Valudio.NetSocket.NETStandard

How does it work?

This library comes with a middleware that you can use in your Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRealTimeComm(); // this will inject all the services
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole();
    app.UseRealTimeComm(); // this will start listening
    if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
}

Once you have set this up the next step is to create your own classes extending SocketServiceBase . You will have 4 methods to override:

// this will be executed whenever a new websocket is open.
public virtual void OnClientInitialized(IClient client) { }
// this will be executed whenever the server receives some message.
public virtual void OnMessageReceived(IClient fromClient, string message) { }
// this will be executed whenever the server sents any message to some client
public virtual void OnMessageSent(IClient toClient, string message, IClient fromClient) { }
// this will be executed whenever a websocket is closed.
public virtual void OnClientClosed(IClient client) { }

Besides those virtual methods you've got some other methods that will be useful to send messages:

// just like the method's name says... sends a message to everyone.
Task SendAllClientsAsync(string message, IClient fromClient = null);
// sends a message to all the clients except the one you pass as a parameter.
Task SendAllClientsExceptAsync(IClient except, string message, IClient fromClient = null);
// sends a message to all the clients except the ones you pass as a parameter.
Task SendAllClientsExceptAsync(IEnumerable<IClient> except, string message, IClient fromClient = null);
// sends a message to a specific client.
Task SendClientAsync(IClient toClient, string message, IClient fromClient = null);

Enabling / Disabling SocketServices

Let's say you want to not use one of your SocketServices (your classes extending SocketServiceBase). You've got the option to mark them with an attribute SocketServiceAttribute:

// setting this attribute to false will make this service not to be used by the middleware.
// this attribute is only necessary to deactivate the service. If not present
[SocketService(false)]
public class HelloTimerService : SocketServiceBase
{
    public HelloTimerService(ISocketManager manager) : base(manager)
    {
        var timer = new System.Threading.Timer(async (o) =>
        {
            await SendAllClientsAsync($"Hello at {DateTime.Now.ToUniversalTime()}");
        }, null, 0, 30000);
        
    }
}

Sending additional parameters to the server

If you want to send additional custom data through the socket connection you can add it to the url as a query string

var url = "ws://domain.com/ws?param1=value&param2=value";
var socket = new WebSocket(url);

You will receive that data in the property AdditionalParameters as a Dictionary<string, StringValues> in the IClient passed as a parameter to the virtual methods of your SocketServices

How to test this

Run one of the example projects and then use Simple Web Socket Client for Chrome, for instance, in order to setup a websocket connection.

Alternatively, if you don't want to run the examples, you can use this url ws://examplenetsocketnetcore.azurewebsites.net. Bear in mind this is a F1 free Azure Machine so it will have to start-up first. Stay calm if first request takes longer than it should. ;D

netsocket's People

Contributors

robertohuertasm avatar vfabregat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.