GithubHelp home page GithubHelp logo

bionstt / aspnetcoresignalr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rafaelfgx/chatservice

0.0 0.0 0.0 52 KB

SignalR in ASP.NET Core.

License: MIT License

HTML 13.97% TypeScript 47.38% CSS 12.53% C# 26.12%

aspnetcoresignalr's Introduction

AspNetCoreSignalR

SignalR in ASP.NET Core.

Tools, Practices and Technologies

  • Cross-Platform (Windows, Linux, macOS)
  • Visual Studio 2017
  • .NET Core 2.2
  • ASP.NET Core 2.2
  • C# 7.3
  • SPA (Single Page Application)
  • Angular 7.2.7
  • Typescript 3.2.4
  • HTML5
  • CSS3
  • SASS (Syntactically Awesome Style Sheets)
  • UIkit
  • Dependency Injection

Screenshot

Screenshot

Run Visual Studio 2017

  1. Open directory AspNetCoreSignalR.Client\ClientApp in command line and execute npm run restore.

  2. Set all projects to start (Multiple startup projects).

  3. Press F5.

  4. Open https://localhost:8020 in more than one browser tab.

Run Command Line

  1. Install .NET Core SDK.

  2. Open directory AspNetCoreSignalR.Client\ClientApp in command line and execute npm run restore.

  3. Open directory AspNetCoreSignalR.Client in command line and execute dotnet run.

  4. Open directory AspNetCoreSignalR.Server in command line and execute dotnet run.

  5. Open https://localhost:8020 in more than one browser tab.

Server

ChatModel.cs

public class ChatModel
{
   public string Message { get; set; }

   public string Name { get; set; }
}

ChatHub.cs

public class ChatHub : Hub
{
   public void SendToAll(ChatModel model)
   {
       Clients.All.SendAsync(nameof(SendToAll), model);
   }
}

Startup.cs

public class Startup
{
   public void Configure(IApplicationBuilder application)
   {
       application.UseCors(x => x.AllowAnyOrigin().AllowCredentials().AllowAnyHeader().AllowAnyMethod());
       application.UseSignalR(x => x.MapHub<ChatHub>(string.Empty));
   }

   public void ConfigureServices(IServiceCollection services)
   {
       services.AddCors();
       services.AddSignalR();
   }
}

Client

ChatService.ts

export class ChatService {
   public sendToAllEvent = new Subject<ChatModel>();
   private connection: HubConnection;

   constructor() {
       this.connection = new HubConnectionBuilder().withUrl(environment.chatHubUrl).build();
       this.connection.on("SendToAll", (model: ChatModel) => { this.sendToAllEvent.next(model); });
       this.startConnection();
   }

   public sendToAll(model: ChatModel) {
       return this.connection.invoke("SendToAll", model);
   }

   private startConnection() {
       this.connection.start().catch(() => { setTimeout(() => { this.startConnection(); }, 5000); });
   }
}

ChatComponent.ts

export class ChatComponent {
   public model = new ChatModel();
   @ViewChild("messages") public messages: ElementRef;

   constructor(private readonly chatService: ChatService) {
       this.chatService.sendToAllEvent.subscribe((model: ChatModel) => {
           this.messages.nativeElement.innerHTML += `<b>${model.name}: </b> ${model.message} <br />`;
       });
   }

   public submit() {
       this.chatService.sendToAll(this.model).then(() => { this.model.message = ""; });
   }
}

aspnetcoresignalr's People

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.