Created
June 29, 2019 17:31
-
-
Save drochgenius/ad4b12a327a488516125e433d7c73a15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { css, CSSResult, LitElement, html, customElement, property, TemplateResult } from 'lit-element'; | |
@customElement('kafka-consumer') | |
export class KafkaConsumer extends LitElement { | |
@property({ type: Array }) | |
private message: string; | |
public static get styles(): CSSResult { | |
return css` | |
div { | |
border: 2px solid black; | |
padding: 1em; | |
font-size: 1.5em; | |
color: blue; | |
background-color: lightyellow; | |
} | |
`; | |
} | |
public constructor() { | |
super(); | |
this.socketConnect(); | |
} | |
protected render(): TemplateResult { | |
return html` | |
<div> | |
<p>${this.message}</p> | |
</div> | |
`; | |
} | |
private socketConnect(): void { | |
const ws = new WebSocket('ws://localhost:3210', ['json']); | |
ws.addEventListener( | |
'open', | |
(): void => { | |
console.log('websocket connection open'); | |
} | |
); | |
// Listen to messages coming from the server over the Web Socket | |
ws.addEventListener('message', this.handleMessage.bind(this)); | |
} | |
private handleMessage(event: MessageEvent): void { | |
console.log('Received:', event.data); | |
this.message = event.data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment