No matter what client SDK you use, this guide will teach you how to connect to your server using a HTTP transporter.
Hint: You can make your life a lot easier by using code generation. Just pointing it out, this guide will basically be equivalent.
Before opening the actual connection, you need to create a Client (this is the object you’ll be using to send requests).
This is an abstraction we put over the actual connection since you could also be connecting to the server over WebSocket. The actual message you send is the same between WebSocket and HTTP.
// Import both the transporter (client version) and the client package.
import (
client "github.com/Liphium/neoroute/client/sdk/v2"
"github.com/Liphium/neoroute/client/transporter/http/v2"
)
// ...
// 1. Parse the URL to your server
url, err := url.Parse("http://example.com")
if err != nil { /* handle error */ }
// 2. Create a new client (there are more options, we just showcase the most important ones)
c := client.NewClient(client.Config{
// ErrorHandler receives errors that cannot be returned to the caller.
// For HTTP though, you should probably handle the errors directly when sending requests.
ErrorHandler: func(err error) {
log.Println("HTTP error", err)
},
// Configure how long requests take before timing out.
// Defaults to 5 seconds if not set, you can customize this per route (see sending docs)
RequestTimeout: 10 * time.Second,
})
// 3. Make the client send HTTP requests
http.ApplyHTTP(c, "POST" /* method used, depends on your server */, url)// Import @liphium/neoroute-ts
// 1. Create a new client
const client = new Client({
// THIS IS REQUIRED.
// This also has the responsibility of converting any error into a string, that
// will be returned inside of the UserError returned by send functions.
errorHandler: (err: Error) => {
console.log("Global HTTP receiver error:", err);
return "Something went wrong"; // The string that will be in UserError
},
// Configure how long requests take before timing out (in milliseconds).
// Defaults to 5 seconds if not set, you can customize this per route (see sending docs)
requestTimeout: 10000,
});
// 2. Let the client send HTTP requests
applyHTTP(client, "POST" /* request method, depends on your server */, "http://example.com");POST since it’s the method that describes this usage of HTTP the best. We send your request, encoded in MessagePack, in the request body and the response from the server lives in the response body to the request. It’s also the default for HTTP transporters in neodebug.Depending on what SDK you use, you can now send requests to your server using HTTP. Follow this guide to learn how.