Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright ${year}-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import { Component, OnInit } from '@angular/core'; |
| 17 | import {LogService, WebSocketService} from 'gui2-fw-lib'; |
| 18 | |
| 19 | const SAMPLE_CUSTOM_DATA_REQ = 'sampleCustomDataRequest'; |
| 20 | const SAMPLE_CUSTOM_DATA_RESP = 'sampleCustomDataResponse'; |
| 21 | |
| 22 | @Component({ |
| 23 | selector: '${artifactId}-app-sample', |
| 24 | templateUrl: './${artifactId}.component.html', |
| 25 | styleUrls: ['./${artifactId}.component.css'] |
| 26 | }) |
| 27 | export class ${appNameCap}${appNameEnd}Component implements OnInit { |
| 28 | private handlers: string[] = []; |
| 29 | private openListener: any; |
| 30 | |
| 31 | constructor( |
| 32 | protected log: LogService, |
| 33 | protected wss: WebSocketService |
| 34 | ) { |
| 35 | this.log.debug('${appNameCap}${appNameEnd}Component constructed'); |
| 36 | } |
| 37 | |
| 38 | ngOnInit() { |
| 39 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 40 | [SAMPLE_CUSTOM_DATA_RESP, (data) => { |
Sean Condon | 27f06da | 2019-05-25 17:02:14 +0100 | [diff] [blame] | 41 | this.log.debug(SAMPLE_CUSTOM_DATA_RESP, data); |
Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame] | 42 | } |
| 43 | ] |
| 44 | ])); |
| 45 | |
| 46 | this.handlers.push(SAMPLE_CUSTOM_DATA_RESP); |
| 47 | |
| 48 | // in case we fail over to a new server, |
| 49 | // listen for wsock-open events |
| 50 | this.openListener = this.wss.addOpenListener(() => this.wsOpen); |
| 51 | } |
| 52 | |
| 53 | wsOpen(host: string, url: string) { |
| 54 | this.log.debug(SAMPLE_CUSTOM_DATA_RESP, ': WSopen - cluster node:', host, 'URL:', url); |
| 55 | // tell the server we are ready to receive topo events |
| 56 | this.wss.sendEvent(SAMPLE_CUSTOM_DATA_REQ, {}); |
| 57 | } |
| 58 | } |