Giter VIP home page Giter VIP logo

marisocketmiddleware's Introduction

MariSocketMiddleware

An ASP.NET Core Middleware Event-based for multiple WebSockets services with easy and low memory usage.

Usage

MariSocketMiddleware is very easy to use, just implement that in your ASP.NET core project.

Service class

using MariSocketMiddleware.Entities.MariEventArgs;
using MariSocketMiddleware.Services;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace MyApi
{
    public class MyWebSocketService : MariWebSocketService
    {
        public MyWebSocketService()
        {
            OnOpen += OpenAsync;
            OnDisconnected += OnDisconnectedAsync;
            OnError += OnErrorAsync;
            OnMessage += OnMessageAsync;
        }

        private Task OnMessageAsync(MessageEventArgs arg)
            => Task.CompletedTask;

        private Task OnErrorAsync(ErrorEventArgs arg)
            => Task.CompletedTask;

        private Task OnDisconnectedAsync(DisconnectEventArgs arg)
            => Task.CompletedTask;

        private Task OpenAsync(OpenEventArgs arg)
            => Task.CompletedTask;

        public override string Path { get; set; } = "/Gateway";

        public override Task<bool> AuthorizeAsync(HttpContext context)
            => Task.FromResult(true);
    }
}

In Startup.cs

using MariSocketMiddleware.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MyApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // Here
            // The generic type or the instance.
            services.AddMariWebSocketService<MyWebSocketService>();

            // ASP.NET Core template.
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // ASP.NET Core template.
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            // Here
            app.UseMariWebSockets();

            // ASP.NET Core template.
            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

Additional

Your service will be injected in the ASP.NET Core D.I, therefore you can get your service instance or get others services in your service.

Getting other dependencie

using MariSocketMiddleware.Entities.MariEventArgs;
using MariSocketMiddleware.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace MyApi
{
    public class MyWebSocketService : MariWebSocketService
    {
        private readonly ILogger<MyWebSocketService> _logger;

        // ASP.NET Core will pass the dependencies in your class ctor.
        public MyWebSocketService(ILogger<MyWebSocketService> logger)
        {
            _logger = logger;
            OnOpen += OpenAsync;
            OnDisconnected += OnDisconnectedAsync;
            OnError += OnErrorAsync;
            OnMessage += OnMessageAsync;
        }

        private Task OnMessageAsync(MessageEventArgs arg)
            => Task.CompletedTask;

        private Task OnErrorAsync(ErrorEventArgs arg)
            => Task.CompletedTask;

        private Task OnDisconnectedAsync(DisconnectEventArgs arg)
            => Task.CompletedTask;

        private Task OpenAsync(OpenEventArgs arg)
            => Task.CompletedTask;

        public override string Path { get; set; } = "/Gateway";

        public override Task<bool> AuthorizeAsync(HttpContext context)
            => Task.FromResult(true);
    }
}

Getting your service

You can just pass your service in other service ctor or do a IServiceProvider#GetService.

using System;
using Microsoft.AspNetCore.Mvc;

namespace MyApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly MyWebSocketService _service1;
        private readonly MyOtherWebSocketService _service2;

        public ValuesController(MyWebSocketService service1, IServiceProvider provider)
        {
            // Direct requesting your service or
            _service1 = service1;

            // Requesting for your provider.
            _service2 = provider.GetService<MyOtherWebSocketService>();
        }

        // GET api/values
        [HttpGet]
        public ActionResult Get()
            => Ok(new string[] { "value1", "value2" });
    }
}

License

MariSocketMiddleware is provided under The MIT License.

marisocketmiddleware's People

Contributors

renanrcp avatar

Stargazers

 avatar

Watchers

 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.