-
Notifications
You must be signed in to change notification settings - Fork 1
/
web-socket.html
119 lines (109 loc) · 3.81 KB
/
web-socket.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!--
Copyright 2017 Gerard Payne
@element web-socket
@author Gerard Payne
@description Creates and manages web socket connections.
<web-socket
url="ws://127.0.0.1:8080/socket"
></web-socket>
It may include optional properties for attaching callbacks to lifecycle events.
<web-socket
url="ws://127.0.0.1:8080/socket"
on-open="subscribeSocket"
on-message="showMessage"
on-error="showError"
on-close="endSocket"
></web-socket>
You can also include optional properties causing the component to attempt to establish a socket when loaded.
<web-socket
url="ws://127.0.0.1:8080/socket"
auto
></web-socket>
and/or optional properties causing the component to log events to the console.
<web-socket
url="ws://127.0.0.1:8080/socket"
debug
></web-socket>
-->
<dom-module id="web-socket">
<style type="text/css">
:host() {
display: none;
}
</style>
<template></template>
</dom-module>
<script type="text/javascript">
Polymer({
is: "web-socket",
properties: {
protocol: {
type: String,
value: ""
},
url: {
type: String
}
},
/* events for subscription by host */
onError: function (eEvent) {
if(!!this.attributes.debug) {console.error(this.id + ": Socket error.", eEvent);}
this.fire('error', eEvent);
},
onOpen: function (eEvent) {
if(!!this.attributes.debug) {console.info(this.id + ": Socket open.", eEvent);}
this.fire('open');
},
onMessage: function (eEvent) {
if(!!this.attributes.debug) {console.info(this.id + ": Socket message.", eEvent);}
this.fire('message', eEvent.data);
},
onClose: function (eEvent) {
if(eEvent.wasClean === true) {
if(!!this.attributes.debug) {console.info(this.id + ": Socket closed.", eEvent);}
this.fire('close', "");
} else {
console.warn("Socket closed.", eEvent.reason);
this.fire('close', eEvent.reason);
}
},
/* methods */
open: function () {
// url must be supplied but we allow browser to handle malformed url errors
if(this.url.length > 0) {
// if protocol is set construct socket with, otherwise construct without
var _socket = this.protocol.length > 0 ? new WebSocket(this.url, this.protocol) : new WebSocket(this.url);
// binds socket events to component events
_socket.onerror = this.onError.bind(this);
_socket.onopen = this.onOpen.bind(this);
_socket.onmessage = this.onMessage.bind(this);
_socket.onclose = this.onClose.bind(this);
// add the socket to the component's scope
this.set('socket', _socket);
} else {
console.error("Please provide protocol and url.", this);
}
},
send: function (oMessage) {
if(!!this.socket && this.socket.readyState === 1) {
if(!!this.attributes.debug) {
console.info(this.id + ": Socket send.", oMessage);
}
this.socket.send(oMessage);
} else {
console.error(this.id + ": Socket not ready.", this.socket);
}
},
close: function () {
if(!!this.socket && this.socket.readyState === 1) {
this.socket.close();
this.set('socket', undefined);
}
},
ready: function () {
if(!!this.attributes.auto) {
this.open();
}
}
});
</script>