-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshared-websocket-component.html
More file actions
104 lines (92 loc) · 2.85 KB
/
shared-websocket-component.html
File metadata and controls
104 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!--
Copyright (c) 2018 Willem Burgers. All rights reserved.
This code may only be used under the BSD style license found at http://wburgers.github.io/LICENSE.txt
For a list of Contributors check https://github.com/wburgers/websocket-component/graphs/contributors
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="websocket-component.html">
<link rel="import" href="websocket-sharing-behavior.html">
<!--
A Polymer wrapper for a websocket connection that is shared among certain instances by a ID.
Example:
<shared-websocket-component socket-id="someID" auto handle-visibility url="wss://echo.websocket.org"></websocket-component>
@element shared-websocket-component
@demo demo/index.html
-->
<dom-module id="shared-websocket-component">
<template>
<style>
:host {
display: none;
}
</style>
</template>
<script>
/**
* @extends {Polymer.Element}
* @appliesMixin WebsocketComponentBehavior
*/
class SharedWebsocketComponent extends WebsocketSharingBehavior( WebsocketComponentBehavior(Polymer.Element) ) {
static get is() {
return "shared-websocket-component";
}
static get properties() {
return {
_wsReadyFired: {
type: Boolean,
value: false
}
};
}
/**
* Open the connection manually. May throw errors when already connected or no url is specified.
*/
open() {
if (!this.url || this.url === "") {
throw new Error("shared-websocket-component(open): no url specified.");
}
if (!this.socketId || this.socketId === "") {
throw new Error("shared-websocket-component(open): no socketId specified.");
}
if (this._ws) {
throw new Error("shared-websocket-component(open): already connected");
}
this._ws = this.getOrCreateSocket(this.url, this.protocols);
this._addEvents();
if(this.status === 1 && !this._wsReadyFired) {
this._wsReadyFired = true;
this.dispatchEvent(new CustomEvent("websocket-ready"));
}
}
/**
* Close the connection manually. Trows an error when the websocket is not connected.
*/
close() {
if (!this._ws) {
throw new Error("shared-websocket-component(close): not connected.");
}
this._ws.close();
this._ws = null;
this.removeWebsocket()
}
_onwsopen() {
super._onwsopen();
if(!this._wsReadyFired) {
this._wsReadyFired = true;
this.dispatchEvent(new CustomEvent("websocket-ready"));
}
}
_onwsclose(event) {
super._onwsclose(event);
const removeEvent = (name, callback) => {
this._ws.removeEventListener(name, callback.bind(this));
};
removeEvent("open", this._onwsopen);
removeEvent("error", this._onwserror);
removeEvent("message", this._onwsmessage);
removeEvent("close", this._onwsclose);
}
}
window.customElements.define(SharedWebsocketComponent.is, SharedWebsocketComponent);
</script>
</dom-module>