App.listen may take host, port, callback now

This commit is contained in:
Alex Hultman 2019-02-10 11:28:33 +01:00
parent dbe4e4547a
commit d9a6b1010f
2 changed files with 20 additions and 4 deletions

View File

@ -171,13 +171,29 @@ template <typename APP>
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) { void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked(); /* Invalid use */
if (args.Length() != 2 && args.Length() != 3) {
/* Throw here */
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "App.listen takes 2 or 3 arguments")));
return;
}
app->listen(port, [&args](auto *token) { auto cb = [&args](auto *token) {
/* Return a false boolean if listen failed */ /* Return a false boolean if listen failed */
Local<Value> argv[] = {token ? Local<Value>::Cast(External::New(isolate, token)) : Local<Value>::Cast(Boolean::New(isolate, false))}; Local<Value> argv[] = {token ? Local<Value>::Cast(External::New(isolate, token)) : Local<Value>::Cast(Boolean::New(isolate, false))};
Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv); Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv);
}); };
if (args.Length() == 2) {
/* Port, callback */
int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
app->listen(port, std::move(cb));
} else {
/* Host, port, callback */
NativeString host(isolate, args[0]);
int port = args[1]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
app->listen(std::string(host.getString().data(), host.getString().length()), port, std::move(cb));
}
args.GetReturnValue().Set(args.Holder()); args.GetReturnValue().Set(args.Holder());
} }

@ -1 +1 @@
Subproject commit 399318f8663cb6c53471c8b7be03d230a13c7a3b Subproject commit 02f20bf0307549cd126b582a2ad1fcf2d0180a58