/*JSON{ "type" : "staticmethod", "class" : "net", "name" : "connect", "generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL)", "params" : [ ["options","JsVar","An object containing host,port fields"], ["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."] ], "return" : ["JsVar","Returns a new net.Socket object"], "return_object" : "Socket" } Create a socket connection */ JsVar *jswrap_net_connect(JsVar *options, JsVar *callback, SocketType socketType) { bool unlockOptions = false; if (jsvIsString(options)) { options = jswrap_url_parse(options, false); unlockOptions = true; } if (!jsvIsObject(options)) { jsError("Expecting Options to be an Object but it was %t", options); return 0; } #ifdef USE_TLS if ((socketType&ST_TYPE_MASK) == ST_HTTP) { JsVar *protocol = jsvObjectGetChild(options, "protocol", 0); if (protocol && jsvIsStringEqual(protocol, "https:")) { socketType |= ST_TLS; } jsvUnLock(protocol); } #endif // Make sure we have a function as callback, or nothing (which is OK too) JsVar *skippedCallback = jsvSkipName(callback); if (!jsvIsUndefined(skippedCallback)) { if (!jsvIsFunction(skippedCallback)) { jsError("Expecting Callback Function but got %t", skippedCallback); jsvUnLock(skippedCallback); return 0; } jsvUnLock(skippedCallback); } else { callback = NULL; } JsVar *rq = clientRequestNew(socketType, options, callback); if (unlockOptions) jsvUnLock(options); if ((socketType&ST_TYPE_MASK) != ST_HTTP) { JsNetwork net; if (networkGetFromVarIfOnline(&net)) { clientRequestConnect(&net, rq); } networkFree(&net); } return rq; }
JsVar *jswrap_http_request(JsVar *options, JsVar *callback) { bool unlockOptions = false; if (jsvIsString(options)) { options = jswrap_url_parse(options, false); unlockOptions = true; } if (!jsvIsObject(options)) { jsError("Expecting Options to be an Object but it was %t", options); return 0; } JsVar *skippedCallback = jsvSkipName(callback); if (!jsvIsFunction(skippedCallback)) { jsError("Expecting Callback Function but got %t", skippedCallback); jsvUnLock(skippedCallback); return 0; } jsvUnLock(skippedCallback); JsVar *rq = httpClientRequestNew(options, callback); if (unlockOptions) jsvUnLock(options); return rq; }