Skip to content

Instantly share code, notes, and snippets.

@Dominent
Created August 3, 2019 15:18
Show Gist options
  • Save Dominent/55b2e0e662bc0a75a1b16cd8cf27cdcf to your computer and use it in GitHub Desktop.
Save Dominent/55b2e0e662bc0a75a1b16cd8cf27cdcf to your computer and use it in GitHub Desktop.
import { Component, OnInit } from '@angular/core';
import WebSocketMessage from './WebSocketMessage';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
public code: string;
private wSocket: WebSocket;
ngOnInit(): void {
const URL = 'ws://localhost:9090/PreviewHub';
this.wSocket = new WebSocket(URL);
this.wSocket.onopen = function ($ev) {
console.log('handshake successfully established.', $ev);
};
this.wSocket.onclose = function ($ev) {
console.log('connection closed', $ev);
};
this.wSocket.onmessage = function ($ev) {
var message = JSON.parse($ev.data) as WebSocketMessage;
this[message.method](...message.parameters);
};
window.addEventListener('keydown', this.onKeyDown.bind(this), false)
}
receivePreviewUrl(previewUrl: string) {
console.log(previewUrl);
}
onSaveHandler() {
let message: WebSocketMessage = {
method :'PushMailAsync',
parameters: [this.code]
}
this.wSocket.send(JSON.stringify(message))
}
onKeyDown($event: KeyboardEvent): void {
let charCode = String.fromCharCode($event.which).toLowerCase();
if ($event.ctrlKey && charCode === 's') {
$event.preventDefault()
this.onSaveHandler()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment