From 430107c065d48568cc1b6391d9166221d92ca13b Mon Sep 17 00:00:00 2001 From: Piotr Pekala Date: Fri, 26 Jul 2019 06:13:42 -0700 Subject: [PATCH] Update project-web-service-handler --- .../project-map/project-map.component.ts | 20 ++++-- .../handlers/project-web-service-handler.ts | 62 +++++++++---------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/app/components/project-map/project-map.component.ts b/src/app/components/project-map/project-map.component.ts index 2e9fe5b2..874a3c2c 100644 --- a/src/app/components/project-map/project-map.component.ts +++ b/src/app/components/project-map/project-map.component.ts @@ -50,6 +50,7 @@ import { InterfaceLabelWidget } from '../../cartography/widgets/interface-label' import { LabelWidget } from '../../cartography/widgets/label'; import { MapLinkNodeToLinkNodeConverter } from '../../cartography/converters/map/map-link-node-to-link-node-converter'; import { ProjectMapMenuComponent } from './project-map-menu/project-map-menu.component'; +import { ToasterService } from '../../services/toaster.service'; @Component({ @@ -65,7 +66,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy { public symbols: Symbol[] = []; public project: Project; public server: Server; - private ws: Subject; + private ws: WebSocket; public isProjectMapMenuVisible: boolean = false; tools = { @@ -113,7 +114,8 @@ export class ProjectMapComponent implements OnInit, OnDestroy { private recentlyOpenedProjectService: RecentlyOpenedProjectService, private movingEventSource: MovingEventSource, private mapScaleService: MapScaleService, - private nodeCreatedLabelStylesFixer: NodeCreatedLabelStylesFixer + private nodeCreatedLabelStylesFixer: NodeCreatedLabelStylesFixer, + private toasterService: ToasterService ) {} ngOnInit() { @@ -228,9 +230,15 @@ export class ProjectMapComponent implements OnInit, OnDestroy { } setUpWS(project: Project) { - this.ws = webSocket(this.projectService.notificationsPath(this.server, project.project_id)); + this.ws = new WebSocket(this.projectService.notificationsPath(this.server, project.project_id)); - this.subscriptions.push(this.projectWebServiceHandler.connect(this.ws)); + this.ws.onmessage = (ev: MessageEvent) => { + this.projectWebServiceHandler.handleMessage(ev); + }; + + this.ws.onerror = (ev: MessageEvent) => { + this.toasterService.error('Connection to host lost.'); + }; } setUpMapCallbacks() { @@ -405,8 +413,8 @@ export class ProjectMapComponent implements OnInit, OnDestroy { this.nodesDataSource.clear(); this.linksDataSource.clear(); - if (this.ws) { - this.ws.unsubscribe(); + if (this.ws.OPEN) { + this.ws.close(); } this.subscriptions.forEach((subscription: Subscription) => subscription.unsubscribe()); } diff --git a/src/app/handlers/project-web-service-handler.ts b/src/app/handlers/project-web-service-handler.ts index 0a9a7e2f..38d9f18f 100644 --- a/src/app/handlers/project-web-service-handler.ts +++ b/src/app/handlers/project-web-service-handler.ts @@ -21,36 +21,36 @@ export class ProjectWebServiceHandler { private drawingsDataSource: DrawingsDataSource ) {} - public connect(ws: Subject) { - const subscription = ws.subscribe((message: WebServiceMessage) => { - if (message.action === 'node.updated') { - this.nodesDataSource.update(message.event as Node); - } - if (message.action === 'node.created') { - this.nodesDataSource.add(message.event as Node); - } - if (message.action === 'node.deleted') { - this.nodesDataSource.remove(message.event as Node); - } - if (message.action === 'link.created') { - this.linksDataSource.add(message.event as Link); - } - if (message.action === 'link.updated') { - this.linksDataSource.update(message.event as Link); - } - if (message.action === 'link.deleted') { - this.linksDataSource.remove(message.event as Link); - } - if (message.action === 'drawing.created') { - this.drawingsDataSource.add(message.event as Drawing); - } - if (message.action === 'drawing.updated') { - this.drawingsDataSource.update(message.event as Drawing); - } - if (message.action === 'drawing.deleted') { - this.drawingsDataSource.remove(message.event as Drawing); - } - }); - return subscription; + public handleMessage(event: MessageEvent) { + console.log(event); + let message = event.data; + if (message.action === 'node.updated') { + console.log('should work'); + this.nodesDataSource.update(message.event as Node); + } + if (message.action === 'node.created') { + this.nodesDataSource.add(message.event as Node); + } + if (message.action === 'node.deleted') { + this.nodesDataSource.remove(message.event as Node); + } + if (message.action === 'link.created') { + this.linksDataSource.add(message.event as Link); + } + if (message.action === 'link.updated') { + this.linksDataSource.update(message.event as Link); + } + if (message.action === 'link.deleted') { + this.linksDataSource.remove(message.event as Link); + } + if (message.action === 'drawing.created') { + this.drawingsDataSource.add(message.event as Drawing); + } + if (message.action === 'drawing.updated') { + this.drawingsDataSource.update(message.event as Drawing); + } + if (message.action === 'drawing.deleted') { + this.drawingsDataSource.remove(message.event as Drawing); + } } }