当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

ngx-signalr-hubservice

Makes using SignalR in Angular 2/4 easy
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 慕容齐智
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

ngx-signalr-hubservice

Makes using SignalR in Angular 2/4 easy.

Getting started

  1. Set up a new project using @angular/cli

  2. Install ngx-signalr-hubservicenpm install --save ngx-signalr-hubservice

  3. Add the jquery and SignalR scripts to your angular-cli.json

    "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/signalr/jquery.signalr.js"]
  4. Import the HubService and add it to your providers in your app.module.ts. Mine looks like this(I've included FormsModule for the demo):

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    
    import { AppComponent } from './app.component';
    
    import { HubService } from 'ngx-signalr-hubservice';
    
    @NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        FormsModule
    ],
    providers: [ HubService ],
    bootstrap: [AppComponent]
    })
    export class AppModule { }
  5. Inject the hub service in (probably) your root component. In this case it's app.component.ts. Make sure you import the service. Here you can make the connection.

    constructor(private hubService: HubService) {
    }
    ...
    ...
    async ngOnInit() {
        // connects to the SignalR server.
    	// passing in null for options will just use /signalr on the current domain as the url
        this.connected = await this.hubService.connect().toPromise();
    }

    For my applications I generally just reference the HubService in my other services(one service for each hub), and expose the hub methods/events through those services. For a demo this works too though.

  6. Define a class that will interact with the SignalR server. For me it's just the root AppComponent.You can use the @Hub decorator on the class to define what hub this class connects to.

    import { Component, OnInit } from '@angular/core';
    import { 
        HubService, 
        Hub, 
        HubSubscription, 
        HubWrapper 
    } from 'ngx-SignalR-hubservice';
    
    import 'rxjs/add/operator/toPromise';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    @Hub({ hubName: 'chatHub' }) // <-- Your hub declaration
    export class AppComponent implements OnInit {
    
        private hubWrapper: HubWrapper;
    
        connected = false;
    
        constructor(private hubService: HubService) {
            this.hubWrapper = hubService.register(this);
        }
    
        async ngOnInit() {
            this.connected = await this.hubService.connect().toPromise();
        }
    
    }
  7. Define a method that the hub will call with @HubSubscription in the same class. You can pass in the method name in the decorator, or just leave it blank. If left blank, the service will use the name of the method that you added the decorator to as the subscription name. NOTE: you generally have to run these callbacks inside angular's zone if you're updating UI. Hopefully future versions you won't have to do this.

    @HubSubscription()
    receiveMessage(param1: any, param2: any) {
        console.log(param1, param2);
    }
  8. For calling methods on the hub, you need to register this class with the hub service.

    Update your constructor to this, and add a new field on your class:

    private hubWrapper: HubWrapper;
    constructor(private hubService: HubService) {
        this.hubWrapper = hubService.register(this);
    }

    Now you can call methods on the hub using this hub wrapper,

    callHubMethod() {
        var result = await this.hubWrapper.invoke<boolean>('methodName', 'someParam').toPromise();
        console.log(result);
    }
  9. You can unregister hub wrappers from the service with hubWrapper.unregister() or hubService.unregister(this);. Generally you wouldn't want to do this because you'll call SignalR from services that exist during the lifetime of your application.

    ngOnDestroy() {
        this.hubWrapper.unregister();
        //or this.hubService.unregister(this);
    }

Hub Groups

You can use the hubGroups parameter on the @Hub decorator if you have two or more SignalR connections being made and want to control which @Hub decorators are applying to which connection.

You can see an example of this here:

@Injectable()
@Hub({ hubName: 'DataHub', hubGroup: 'group1' })
export class DataHubService {

    constructor (@Inject(HUB_SERVICE_GROUP1) private hubService: HubService) {
        this.hubWrapper = this.hubService.register(this);
        this.hubService.connect('http://localhost:81/signalr', { hubGroup: 'group1' }).toPromise();
    }

}

@Injectable()
@Hub({ hubName: 'EvaluationHub', hubGroup: 'group2' })
export class EvaluationHubService {

    constructor (@Inject(HUB_SERVICE_GROUP2) private hubService: HubService) {
        this.hubWrapper = this.hubService.register(this);
        this.hubService.connect('http://localhost:82/signalr', { hubGroup: 'group2' }).toPromise();
    }

}

Note the @Inject decorator on the constructors of the services. You need to specify which connection you want to inject into your service if you're using multiple HubServices. Remember to provide your HubService's correctly too with the proper InjectionToken

// Probably at the top of your NgModule
export let HUB_SERVICE_GROUP1 = new InjectionToken<HubService>("hubservice.group1");
export let HUB_SERVICE_GROUP2 = new InjectionToken<HubService>("hubservice.group2");

...

// In your NgModule decorator
providers: [
    { provide: HUB_SERVICE_GROUP1, useValue: new HubService() }
    { provide: HUB_SERVICE_GROUP2, useValue: new HubService() }
]

Notes

  • If you want to get the underlying SignalR instances, you can access them through HubService.connection for the SignalR connection instance($.connection). You can access the SignalR hub instances for the individual hubs through HubWrapper.hub.

  • If you pass attemptReconnects as true to HubService.connect options parameter, any invoke calls on your HubWrappers will defer until the HubService reconnects. They will most likely not error.

  • 一.认识SingalR 1.Http协议是浏览器端主动发请求,服务器不能主动发起请求。可以使用ajax或者原生websocket开发,但是难度大。但是使用SignalR,简化了WebSocket开发。 2.SignalR集线器类(SignalR持久连接)是底层机制。 3.可以实现即时通讯。SignalR有三种传输模式:LongLooping(长轮询)、WebSocket(HTML5的WEB套接字)

  • int ngx_cdecl main(int argc, char *const *argv) { .... #if !(NGX_WIN32) if (ngx_init_signals(cycle->log) != NGX_OK) { // 服务器启动时进行初始化信号处理 return 1; } if (!ngx_inherited && ccf

  • 1.描述   增加对每请求的响应时间的统计:在stub status模块中增加了自Tengine启动以来所有请求的总响应时间(request_time),   单位为ms,可以用来统计一段时间的平均RT(response time):   Active connections: 1   server accepts handled requests request_time   1140 1140

  • SignalR在ASP.NET MVC 4.5中出现了,ASP.NET MVC开始支持SignalR。那么什么是SignalR?有什么用途? 1.什么是SignalR? SignalR是一个基于ASP.NET平台构建,利用JavaScript或者WebSockets,实现在客户端和服务端异步通信的框架。   由 David Fowler 和Damien Edwards 维护,开源代码的地址在GIT

 相关资料
  • SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话。 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换;它将继续,直到明确关闭。 对话通过永久连接进行,允许客户端向服务器发送多个消息,并允许服务器做出相应答复,值得注意的是,还允许服务器向客户端发送异步消息。

  • 问题内容: 我有一个像在SignalR 1.1.2上运行的应用程序那样的正常聊天,它将被放置在负载平衡的服务器上,因此我需要实现一个底板解决方案来同步服务器。 这是一个ASP.NET MVC4 .NET 4.5应用程序。使用 SignalR集线器 ,而不是持久连接。该应用程序在客户端利用AngularJS来处理ui绑定和更新。 我已经按照此处列出的步骤来实现sql服务器背板,并使用了此处概述的从1

  • 我在Windows Server 2012和IIS8上使用SignalR V2.0,我通过以下文章在服务器上启用了WebSockets,http://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-websocket-protocol-support 但是signalR首先尝试使用websockets,然后将传输更改为serverSe

  • ngx-weui 是一个使用 Angular 构建的 WeUI 组件。 在线示例以及API文档。

  • ngx-fastdfs 是 nginx + lua +fastdfs 实现分布式图片实时动态压缩。 install 进入docker目录docker build -t  fastdfs:dev . 使用 docker -idt -p 80:80 fastdfs:dev /bin/bash进入容器执行/etc/rc.local 测试 进入容器执行test目录下的./test.sh或者直接执行下面脚本

  • ngx-markdown ngx-markdown is an Angular library that combines... Marked to parse markdown to HTML Prism.js for language syntax highlight Emoji-Toolkit for emoji support KaTeX for math expression rende