Skip to content

Instantly share code, notes, and snippets.

@MrTiggr
Created November 11, 2014 14:40
Show Gist options
  • Select an option

  • Save MrTiggr/1055b5730e42c8ed087e to your computer and use it in GitHub Desktop.

Select an option

Save MrTiggr/1055b5730e42c8ed087e to your computer and use it in GitHub Desktop.
Polymer("realtime-websocket", {
/**
* Should both sent and received data be interpreted as json and encoded/decoded ? Defaults to false.
* @public
* @type {Boolean}
*/
json: false,
/**
* Should sent data be json-encoded, even if {{json}} is falsy ? Defaults to false.
* @public
* @type {Boolean}
*/
jsonSend: false,
/**
* Should received data be json-decoded, even if {{json}} is falsy ? Defaults to false.
* @type {Boolean}
*/
jsonReceive: false,
/**
* WebSocket object.
* @private
* @type {WebSocket}
*/
_ws: null,
/**
* Send data through the WebSocket connection. Will optionally be json-encoded if {{json}} or {{jsonSend}} is truthy.
* @public
* @param {*} data The data to send.
*/
data:null,
send: function(data) {
if(!this._ws) {
throw new Error("realtime-websocket.send(...): not connected.");
}
if(this.json || this.jsonSend) {
data = JSON.stringify(data);
}
this._ws.send(data);
},
/**
* Close the current connection. Optionally provide with a reason.
* @public
* @param {[String]} Optional closing reason to provide the server with.
*/
close: function(reason) {
if(this._ws) {
this._ws.close(reason);
this._ws = null;
}
},
/**
* Underlying connection readyState getter.
* @see {@link http://www.w3.org/TR/websockets/#dom-websocket-readystate}
* @public
* @type {Number}
*/
get readyState() {
if(this._ws) {
return this._ws.readyState;
}
else {
return -1;
}
},
/**
* {{url}} change handler.
* @private
*/
urlChanged: function() {
this._connect();
},
/**
* Create a connection to the remote server identified by {{url}}.
* @private
*/
_connect: function() {
if(!this.url) {
throw new Error("realtime-websocket.connect(...): no url.");
}
if(this._ws) {
throw new Error("realtime-websocket.connect(...): already connected.");
}
this._ws = new WebSocket(this.url);
this._ws.onopen = this._onwsopen.bind(this);
this._ws.onerror = this._onwserror.bind(this);
this._ws.onmessage = this._onwsmessage.bind(this);
this._ws.onclose = this._onwsclose.bind(this);
},
/**
* WebSocket open event handler. Re-fires to the realtime-websocket element.
* @private
*/
_onwsopen: function(ws) {
this.fire("open",ws);
},
/**
* WebSocket error event handler. Re-fires to the realtime-websocket element.
* @private
*/
_onwserror: function(event) {
this.fire("error", { code: event.code, reason: event.reason });
},
/**
* WebSocket message event handler. Re-fires to the realtime-websocket element, after optionally json-parsing the payload if {{json}} or {{jsonReceive}} is truthy.
* @private
*/
_onwsmessage: function(event) {
var data = event.data;
if(this.json || this.jsonReceive) {
data = JSON.parse(data);
}
this.data=data;
this.fire("wsmessage", { data: data });
},
/**
* WebSocket close event handler.
* @private
*/
_onwsclose: function(event) {
this.fire("error", { code: event.code, reason: event.reason });
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment