如何在Angular 2中使用ng2 SignalR连接到SignalR服务器的IP地址

时间:2019-04-30 02:19:43

标签: angular signalr

我已经使用ng2创建了一个SignalR应用程序。到目前为止,它运行正常。我的问题在于它可以连接到硬编码的IP地址。我需要动态提供IP地址,以便可以在给定的时间连接到任何服务器。在ng2的文档中,SignalR配置位于app.module.ts中。这意味着无法在运行时对其进行任何更改。我正在连接“ tabs1”页面。请参阅下面的代码,了解如何实现此功能。

app.module.ts

import { SignalRModule } from 'ng2-signalr';
import { SignalRConfiguration } from 'ng2-signalr';

export function createConfig(): SignalRConfiguration {
  const config = new SignalRConfiguration();
  config.hubName = 'ServerHub';
  config.qs = { user: 'Lane' };
  config.url = 'http://localhost:8089/'; <=== This is the URL of the server that I need to dynamically change
  config.executeEventsInZone = true;
  config.executeErrorsInZone = false;
  config.executeStatusChangeInZone = true;
  return config;
}

connection.service.ts

import { Injectable, EventEmitter } from '@angular/core';
import { SignalR, IConnectionOptions, BroadcastEventListener } from 'ng2-signalr';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ConnectionService {
  messageReceived: EventEmitter<string> = new EventEmitter();
  messageFromServer = new Subject<string>();
  options: IConnectionOptions = { hubName: 'ServerHub' };
  onMessageReceived$ = new BroadcastEventListener<string>('Addmessage');
  onServerBroadcast$ = new BroadcastEventListener('serverBroadcast');
  connection: any = null;
  public message = '';

  constructor(
    private signalR: SignalR
  ) { }

  public startConnection(): void {
    this.connection = this.signalR.createConnection();
    this.connection.start().then((c: { errors: { hasError: any; }; }) => {
      if(!c.errors.hasError) {
        // this.connection.listen(this.onMessageReceived$);
        this.connection.listen(this.onServerBroadcast$);

        this.onMessageReceived$.subscribe((message: string) => {
          console.log('asdfasdf' + message);
        });

        this.onServerBroadcast$.subscribe((message: any) => {
          this.message = this.message + message;
          this.messageFromServer.next(this.message);
        });
      }
    });
  }

  public MessageFromClient(message: string): void {
    this.connection.invoke('messageFromClient', message)
      .then((data: any) => {
        console.log(data);
      });
  }

  public getMessage() {
    return this.message;
  }
}

tab1.component.ts

import {
  SignalR,
  BroadcastEventListener,
  IConnectionOptions
} from 'ng2-signalr';
import { NgForm } from '@angular/forms';
import { ConnectionService } from '../../_services/connection.service';
import { Subscription } from 'rxjs';

 message = '';
  options: IConnectionOptions = { hubName: 'ServerHub' };
  onMessageSent$ = new BroadcastEventListener<string>('addMessage');
  private messageFromServerSubscription: Subscription;
  ipAddress: string;

onSubmit(form: NgForm){
    if(!form.valid) {
      return;
    }
    this.ipAddress = form.value;
    console.log(this.ipAddress);
    this.cs.startConnection(); <=== This is how I start the connection
  }

0 个答案:

没有答案