A Polymer 2.0 element to ease the usage of WebSockets.
<web-socket auto
url="{{url}}"
protocols="{{protocols}}"
state="{{state}}"
last-request="{{request}}"
last-response="{{response}}"
last-error="{{error}}"
on-error="_handleError"
on-message="_handleMessage"
on-open="_handleOpen"
on-close="_handleClose"
verbose>
</web-socket>
The above example illustrates the usage of <web-socket>
in a Polymer app.
Attributes explained:
- auto = Enables auto connection with page load
- url = The URL to which to connect
- handle-as = Empty for the plain message or
json
to get it parsed - protocols = An optional property to provide a single protocol string or an array of protocol strings.
- state = The current state of the WebSocket connection. Notifies about state changes: WebSocket.readyState
- last-request = The most recent request made by this web-socket element.
- last-response = The most recent response received by this web-socket element.
- last-error = The most recent error received by this web-socket element. If any error occurred.
- verbose = Enables verbose mode
Events:
- onError = Sends an event when the socket returns an error
- onMessage = Sends an event when a new frame is received from the socket
- onOpen = When the connection is established
- onClose = When the connection is closed