Giter VIP home page Giter VIP logo

signalr.easyuse's Introduction

SignalR.EasyUse

SignalR

GitHub last commit GitHub code size in bytes

GitHub search hit counter Nuget Nuget Nuget

What is it?

This is a framework that eliminates a whole class of contract non-compliance errors when writing client and server applications in C# using SignalR

How to use?

Create interface

  • Create a project in your solution that will contain contracts.
  • Add Nuget Package in this project:
    Install-Package SignalR.EasyUse.Interface

Define server methods

  • Create an interface with server methods in this project. The name of the interface methods will be passed as an identifier, and the method parameters will be passed as-is. The type of method return value must be Task or Task<TResult>.
  • It can be inherited from SignalR.EasyUse.Interface.IServerMethods so that you can easily distinguish them and use them for their intended purpose. But inheritance is not necessary.

Example:

public interface IChatHub: IServerMethods
{
    Task SendMessage(string user, string message);
    Task<List<User>> Login(string name, byte[] photo);
}

Define client methods

  • The client methods that the server calls are defined slightly differently. You need to create a class for each method. The class name will be passed as an identifier. All its public properties will be passed as parameters in the order in which they are declared in the class.
  • These classes can be inherited from SignalR.EasyUse.Interface.IClientMethod so that you can easily distinguish them and use them for their intended purpose. But inheritance is not necessary.

Example:

public class ReceiveMessage: IClientMethod
{
    public string User { get;set; }
    public string Message{ get;set; }
}

Use interfaces in server project

  • Add a link to the project with contracts.
  • Now you need to implement your server interface. Since you are required to follow the interface description, the hub will comply with the contract.
  • Wherever you need to send messages to clients, you can now use strongly typed calls using the extension method:
async Task SendAsync<T>(this IClientProxy clients, T payload)

Example:

//This class implements the IChatHub interface
public class ChatHub : Hub, IChatHub
{
    //This method from IChatHub interface
    public async Task SendMessage(string user, string message)
    {
        //EasyUse send message
        await Clients.All.SendAsync(new ReceiveMessage
        {
            User = user,
            Message = message,
		});
		
		//The code above corresponds to this
        //Native send message
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

Use interfaces in client project

  • Add a link to the project with contracts.
  • Сreate a connection to the hub, as usual:
_connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:53353/ChatHub")
                .Build();
  • Then create a dynamic proxy for connection based on the interface:
var hub = _connection.CreateHub<IChatHub>();
  • Use a proxy to call server methods:
var users = await hub.Login(UserName, Photo);
await hub.SendMessage(UserName, MessageText);

This is what a call looks like without using the EasyUse framework:

var users = await connection.InvokeCoreAsync<List<User>>("Login", new object[] { UserName, Photo });
await _connection.InvokeAsync("SendMessage", UserName, MessageText);
  • To subscribe to a message from the server use the extension method:
void Subscribe<T>(this HubConnection connection, Action<T> action)

Example:

_connection.Subscribe<ReceiveMessage>(data =>
{
	var newMessage = $"{data.User}: {data.Message}";
	Messages.Add(newMessage);
});

This is what a subscribe looks like without using the EasyUse framework:

_connection.On<string, string>("ReceiveMessage", (user, message) =>
{
	var newMessage = $"{user}: {message}";
	Messages.Add(newMessage);
});

Get a sample!

If you want to touch the working project right away, see examples of ready-made applications that use the framework:

Communication

Any suggestions and comments are welcome. If you want to contact me, use Telegram

signalr.easyuse's People

Contributors

ihouger avatar kibnet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ihouger sumithxp

signalr.easyuse's Issues

Add tests

  • Write basic tests
  • Configure CI for run tests

How to solve the big size problem?

There seems to be a problem with the large size of the value to be sent. And I think the timeout is about 30 seconds, is there any way to increase this time?

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.