Register message endpoints.
Store endpoint origins only.
This commit is contained in:
parent
8595053d28
commit
49a2985515
2 changed files with 26 additions and 5 deletions
|
@ -111,6 +111,8 @@ Example:
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import URL from 'url';
|
||||||
|
|
||||||
const WIDGET_API_VERSION = '0.0.1'; // Current API version
|
const WIDGET_API_VERSION = '0.0.1'; // Current API version
|
||||||
const SUPPORTED_WIDGET_API_VERSIONS = [
|
const SUPPORTED_WIDGET_API_VERSIONS = [
|
||||||
'0.0.1',
|
'0.0.1',
|
||||||
|
@ -160,12 +162,20 @@ function stopListening() {
|
||||||
* @param {string} endpointUrl Widget wurl origin (protocol + (optional port) + host)
|
* @param {string} endpointUrl Widget wurl origin (protocol + (optional port) + host)
|
||||||
*/
|
*/
|
||||||
function addEndpoint(widgetId, endpointUrl) {
|
function addEndpoint(widgetId, endpointUrl) {
|
||||||
const endpoint = new WidgetMessageEndpoint(widgetId, endpointUrl);
|
const u = URL.parse(endpointUrl);
|
||||||
if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) {
|
if (!u || !u.protocol || !u.host) {
|
||||||
if (global.mxWidgetMessagingMessageEndpoints.filter(function(ep) {
|
console.warn("Invalid origin");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const origin = u.protocol + '//' + u.host;
|
||||||
|
const endpoint = new WidgetMessageEndpoint(widgetId, origin);
|
||||||
|
if (global.mxWidgetMessagingMessageEndpoints) {
|
||||||
|
if (global.mxWidgetMessagingMessageEndpoints.some(function(ep) {
|
||||||
return (ep.widgetId === widgetId && ep.endpointUrl === endpointUrl);
|
return (ep.widgetId === widgetId && ep.endpointUrl === endpointUrl);
|
||||||
}).length > 0) {
|
})) {
|
||||||
// Message endpoint already registered
|
// Message endpoint already registered
|
||||||
|
console.warn("Endpoint already registered");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
global.mxWidgetMessagingMessageEndpoints.push(endpoint);
|
global.mxWidgetMessagingMessageEndpoints.push(endpoint);
|
||||||
|
@ -179,10 +189,19 @@ function addEndpoint(widgetId, endpointUrl) {
|
||||||
* @return {boolean} True if endpoint was successfully removed
|
* @return {boolean} True if endpoint was successfully removed
|
||||||
*/
|
*/
|
||||||
function removeEndpoint(widgetId, endpointUrl) {
|
function removeEndpoint(widgetId, endpointUrl) {
|
||||||
|
const u = URL.parse(endpointUrl);
|
||||||
|
if (!u || !u.protocol || !u.host) {
|
||||||
|
console.warn("Invalid origin");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn("Origin url:", u);
|
||||||
|
const origin = u.protocol + '//' + u.host;
|
||||||
|
|
||||||
if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) {
|
if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) {
|
||||||
const length = global.mxWidgetMessagingMessageEndpoints.length;
|
const length = global.mxWidgetMessagingMessageEndpoints.length;
|
||||||
global.mxWidgetMessagingMessageEndpoints = global.mxWidgetMessagingMessageEndpoints.filter(function(endpoint) {
|
global.mxWidgetMessagingMessageEndpoints = global.mxWidgetMessagingMessageEndpoints.filter(function(endpoint) {
|
||||||
return (endpoint.widgetId != widgetId || endpoint.endpointUrl != endpointUrl);
|
return (endpoint.widgetId != widgetId || endpoint.endpointUrl != origin);
|
||||||
});
|
});
|
||||||
return (length > global.mxWidgetMessagingMessageEndpoints.length);
|
return (length > global.mxWidgetMessagingMessageEndpoints.length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,6 +153,7 @@ export default React.createClass({
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
WidgetMessaging.startListening();
|
WidgetMessaging.startListening();
|
||||||
|
WidgetMessaging.addEndpoint(this.props.id, this.props.url);
|
||||||
window.addEventListener('message', this._onMessage, false);
|
window.addEventListener('message', this._onMessage, false);
|
||||||
this.setScalarToken();
|
this.setScalarToken();
|
||||||
},
|
},
|
||||||
|
@ -206,6 +207,7 @@ export default React.createClass({
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
WidgetMessaging.stopListening();
|
WidgetMessaging.stopListening();
|
||||||
|
WidgetMessaging.removeEndpoint(this.props.id, this.props.url);
|
||||||
window.removeEventListener('message', this._onMessage);
|
window.removeEventListener('message', this._onMessage);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue