Skip to content
Snippets Groups Projects
samp.service.ts 2.05 KiB
Newer Older
  • Learn to ignore specific revisions
  • François Agneray's avatar
    François Agneray committed
    /**
     * This file is part of Anis Client.
     *
     * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    import { Injectable } from '@angular/core';
    
    import { Observable } from 'rxjs';
    
    
    import { AppConfigService } from 'src/app/app-config.service';
    
    François Agneray's avatar
    François Agneray committed
    
    declare var samp: any;
    
    /**
     * @class
     * @classdesc Samp service.
     */
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
    @Injectable()
    
    François Agneray's avatar
    François Agneray committed
    export class SampService {
        private connector = null;
        
    
        constructor(private config: AppConfigService) {
            const baseUrl = window.location.protocol + "//" + window.location.host + this.config.baseHref;
    
    François Agneray's avatar
    François Agneray committed
            const meta = {
                "samp.name": "ANIS",
                "samp.description.text": "AstroNomical Information System",
                "author.email": "cesamsi@lam.fr",
                "author.affiliation": "CeSAM, Laboratoire d'Astrophysique de Marseille",
                "home.page": "https://anis.lam.fr",
                "samp.icon.url": baseUrl + "/assets/cesam_anis40.png"
            };
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            this.connector = new samp.Connector("anis-client", meta);
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Register to Samp.
         *
         * @return Observable<any>
         */
        register(): Observable<any> {
    
    François Agneray's avatar
    François Agneray committed
            return new Observable(observer => {
                samp.register(this.connector.name, (conn) => {
                    this.connector.setConnection(conn);
                    observer.next(true);
                    observer.complete();
                }, () => {
                    observer.error(new Error('Oups!'));
                    observer.complete();
                });
            });
        }
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Disconnect to Samp.
         *
         * @return Observable<any>
         */
    
    François Agneray's avatar
    François Agneray committed
        unregister(): void {
            this.connector.unregister();
        }
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        /**
         * Disconnect to Samp.
         *
         * @param  {string} mtype - Message type.
         * @param  {string} url - URL.
         */
    
    François Agneray's avatar
    François Agneray committed
        broadcast(mtype: string, url: string): void {
            const message = new samp.Message(mtype, {"url": encodeURI(url)});
            this.connector.connection.notifyAll([message]);
        }
    }