diff --git a/src/nodes/network.js b/src/nodes/network.js index 0deef5c79..9f2f62de1 100755 --- a/src/nodes/network.js +++ b/src/nodes/network.js @@ -362,4 +362,61 @@ }; LiteGraph.registerNodeType("network/sillyclient", LGSillyClient); + +//HTTP Request +function HTTPRequestNode() { + this.addInput("request", LiteGraph.ACTION); + this.addInput("url", "string"); + this.addProperty("url", "string"); + this.addOutput("ready", LiteGraph.EVENT); + this.addOutput("data", "string"); + this._data = null; + this._fetching = null; +} + +HTTPRequestNode.title = "HTTP Request"; +HTTPRequestNode.desc = "Fetch data through HTTP"; + +HTTPRequestNode.prototype.fetch = function() +{ + this.boxcolor = "#FF0"; + var that = this; + this._fetching = fetch(url) + .then(resp=>{ + if(!resp.ok) + { + this.boxcolor = "#F00"; + that.trigger("error"); + } + else + { + this.boxcolor = "#0F0"; + return resp.text(); + } + }) + .then(data=>{ + that._data = data; + that._fetching = null; + that.trigger("ready"); + }); +} + +HTTPRequestNode.prototype.onAction = function(evt) +{ + if(evt == "request") + this.fetch(); +} + +HTTPRequestNode.prototype.onExecute = function() { + this.setOutputData(1, this._data); +}; + +HTTPRequestNode.prototype.onGetOutputs = function() { + return [["error",LiteGraph.EVENT]]; +} + +LiteGraph.registerNodeType("network/httprequest", HTTPRequestNode); + + + })(this);