/nativescript-signalr-core

Nativescript Plugin for signalr core

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Nativescript Signalr Core

Plugin for signalr core in NativeScript All my code is LICENSED under the MIT License

Requirements

This plugin dependent on NativeScript WebSockets plugin thanks for nathan@master-technology.com nativescript-websockets

Installation

tns plugin add nativescript-websockets
npm install nativescript-signalr-core --save

Android

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<application android:usesCleartextTraffic="true">

How To use nativescript signalr core ?

main.tns.ts
    declare var require;
    var WebSocket = require('nativescript-websockets');
Home.component.ts

In NativeScript + Angular 7

import {Component, OnInit, NgZone, ChangeDetectorRef} from '@angular/core';
import { SignalrCore } from 'nativescript-signalr-core/angular';

@Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
      header: SignalRCoreRHeaders;

    constructor(private zone: NgZone, private cd: ChangeDetectorRef) {
        this.signalrCore = new SignalrCore();
        this.header = new SignalRCoreRHeaders('Authorization', 'myToken');    //(this.header Optional)
        this.signalrCore.on('connected', (data) => {
             console.log('connected');
        });

    }
    connectToServer() {
        this.signalrCore.start('http://server.com/ChatHub', this.header).then((isConnected: boolean) => {
            console.log('isConnected? ', isConnected);
        });
    }

    invoke() {
        this.signalrCore.invoke('initializeDeviceAsync', '');
    }

    stop() {
        this.signalrCore.close().then((res) => {
            console.log('closed...', res);
        });
    }

API

.start(url: string, header?: SignalRCoreRHeaders): Promise
.on(event: string, data: any) : args
.close()
.invoke(...args): (data, date)
.getStatus$(): observable<{id: number, name: string}>
.getStatus(): string<{id: number, name: string}>
SignalRCoreRHeaders = { key: string, value: string }