Front End
#
Open Match Front End Edgegap's tutorial (basic)The Front End component of Open Match is the shield between your players and Open Match. Every player's request will pass by it. You can validate any data in this service. It is also in this component that you would validate that the data received is valid.
#
Prerequisites#
Create Your project- Go
- C# (HTTP)
Every Open Match service should be in its own project. Create a new folder named front-end for the OpenMatch front-end component.
In this folder, run the following command.
And create a file named main.go
where the core logic of the component will be.
Every Open Match service should be in its own project. Create a new folder named front-end for the OpenMatch front-end component.
In this folder, run the following command.
If you don't use Dotnet version 6.0 or greater, you will generate a more complex project. You will be able to follow the tutorial. However, the structure will be a bit more complex. You will have to work in Startup.cs file
#
Create a REST API- Go
- C# (HTTP)
The first thing that we need for our front end service is a REST API. We decided to go with a REST API instead of gRPC protocol for the front end service because we thought it would be simpler
In this tutorial, we will use the framework ECHO for our REST API since it's lightweight and fast. You could use any other framework if you want.
Let's start by installing the dependencies for ECHO and Open Match
Now, in your main.go file, you can add the following code. This will create a web server that will listen on port 51504
You can try it by starting your app with go run ./main.go
and go to http://localhost:51504/
The API should be almost done since it's been generated by the previous step! We need to change the configuration. You should change the configuration in Properties/launchSettings.json to the following.
Then, we will update Program.cs. We will add a logger, enable forwarded IP from Proxy server and add a middleware to catch unhandled exceptions.
You can try it by starting your app with dotnet run
and go to http://localhost:51504/
#
Create an Open Match ticket- Go
- C# (HTTP)
Open Match works using tickets. To let your game request a ticket, we will create an API endpoint.
Let's add a API endpoint HTTP POST on /v1/tickets. This endpoint will receive a JSON body that contains the data for the matchmaker. In this tutorial, we will keep it simple. We will receive a game mode (mode.ranked, mode.casual, mode.private). Your main function should look like this.
Now, we will create the function createTicket. In this function, we will extract the ticket's data from the request, send the data to Open Match Front End service via gRPC and send back the ticket that has been created.
Don't forget to change your import!
Now we can test our new endpoint. Let's start our app and try it.
At this point, if you send a request to create a ticket, you should have this error on the server side.
Was not able to create a ticket, err: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp: lookup open-match-frontend: no such host"
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster. If you want to try it now, you can expose the service and change the variable openMatchFrontendService by 127.0.0.1:50504. Keep in mind that this is TEMPORARY.
To expose the service run
Restart your app.
Now, if you send a request, everything should be fine, and your ticket should be created.
To test this functionality, make a POST
request to http://localhost:51504/v1/tickets
with the following command.
- Bash
- Powershell
Open Match works using tickets. To let your game request a ticket, we will create an API endpoint.
Let's add a API endpoint HTTP POST on /v1/tickets. This endpoint will receive a JSON body that contains the data for the matchmaker. In this tutorial, we will keep it simple. We will receive a game mode (mode.ranked, mode.casual, mode.private). Your main function should look like this.
Now, we will create a file named Models.cs. This file will contain all the models used for HTTP calls.
Now, we will create a file named Core.cs. This file will contain all the classes that have logic inside.
In Core.cs, let's create a class named Constant. In this class, we will store all the constants for our front end.
In Core.cs, let's create a class named CreateTicket with a function Handle. In this function, we will extract the ticket's data from the request, send the data to Open Match Front End service via HTTP and send back the ticket that has been created.
Now we can test our new endpoint. Let's start our app and try it.
At this point, if you send a request to create a ticket, you should have this error on the server side.
No such host is known. (open-match-frontend:51504)
System.Net.Http.HttpRequestException: No such host is known. (open-match-frontend:51504) ...
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster.
To test this functionality, make a POST
request to http://localhost:51504/v1/tickets
with the following command.
- Bash
- Powershell
#
Get a ticket- Go
- C# (HTTP)
Let's add a API endpoint HTTP GET on /v1/tickets/:ticketId. This endpoint will return the data about a ticket with a given ID. Your main function should look like this.
Now, we will create the function getTicket. In this function, we will fetch the ticket from OpenMatch Front End service via gRPC and send it back to the client.
Now we can test our new endpoint. Let's start our app try it.
Was not able to get a ticket, err: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp: lookup open-match-frontend: no such host"
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster. If you want to try it now, you can expose the service and change the variable openMatchFrontendService by 127.0.0.1:50504. Keep in mind that this is TEMPORARY.
To expose the service run
Restart your app.
Now, if you send a request, everything should be fine, and your ticket should be returned if it exists.
To test this functionality, make a GET
request to http://localhostL51504/v1/tickets/<ticket id>
, changing <ticket id>
for an ID.
- Bash
- Powershell
Let's add a API endpoint HTTP GET on /v1/tickets/:ticketId. This endpoint will return the data about a ticket with a given ID. Your main function should look like this.
In Core.cs, let's create a class named GetTicket with a function Handle. In this function, we will fetch the ticket from OpenMatch Front End service via HTTP and send it back to the client.
Now we can test our new endpoint. Let's start our app and try it.
At this point, if you send a request to create a ticket, you should have this error on the server side.
No such host is known. (open-match-frontend:51504)
System.Net.Http.HttpRequestException: No such host is known. (open-match-frontend:51504) ...
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster.
To test this functionality, make a GET
request to http://localhost:51504/v1/tickets/<ticket id>
changing <ticket id>
for an ID.
- Bash
- Powershell
#
Delete a ticket- Go
- C# (HTTP)
Let's add a API endpoint HTTP DELETE on /v1/tickets/:ticketId. This endpoint will delete the ticket if it exists. Your main function should look like this.
Now, we will create the function deleteTicket. In this function, we will delete the ticket from the OpenMatch Front End service via gRPC and send it back to the client.
Now we can test our new endpoint. Let's start our app try it.
Was not able to delete a ticket, err: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp: lookup open-match-frontend: no such host"
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster. If you want to try it now, you can expose the service and change the variable openMatchFrontendService by 127.0.0.1:50504. Keep in mind that this is TEMPORARY.
To expose the service run
Restart your app.
Now, if you send a request, everything should be fine and your ticket should be deleted if it exists.
To test this functionality, make a DELETE
request to http://localhost:51504/v1/tickets/<ticket id>
changing <ticket id>
for an ID.
- Bash
- Powershell
Let's add a API endpoint HTTP DELETE on /v1/tickets/:ticketId. This endpoint will delete the ticket if it exists. Your main function should look like this.
In Core.cs, let's create a class named DeleteTicket with a function Handle. In this function, we will delete the ticket from OpenMatch Front End service via HTTP and send it back to the client.
Now we can test our new endpoint. Let's start our app and try it.
At this point, if you send a request to create a ticket, you should have this error on the server side.
No such host is known. (open-match-frontend:51504)
System.Net.Http.HttpRequestException: No such host is known. (open-match-frontend:51504) ...
This means that your application was not able to contact the OpenMatch Front End service. It's normal. The service is not exposed outside of the Kubernetes cluster.
To test this functionality, make a DELETE
request to http://localhost:51504/v1/tickets/<ticket id>
changing <ticket id>
for an ID.
- Bash
- Powershell
#
Containerize your service- Go
- C# (HTTP)
Before the Containerization, don't forget to change back openMatchFrontendService to open-match-frontend:50504 if you changed it.
Containerization is powerful. You can learn more about it on Edgegap's documentation or Docker. Our container will use golang:1.16 for its base image.
Create a file named Dockerfile and write this inside.
Containerization is powerful. You can learn more about it on Edgegap's documentation or Docker. Our container will use mcr.microsoft.com/dotnet/aspnet:6.0 for its base image.
Create a file named Dockerfile and write this inside.
You can now build your image using.
You can run it using this command, but without all the other services, it will not work.
#
ConclusionYou now have a basic Open Match Front End REST API service that can be called from your game. This is not production-ready. However, you should be able to grasp the basics of Open Match Front End. You also created a Docker image that can be used later.