-
-
Notifications
You must be signed in to change notification settings - Fork 307
Web Server Extending With MVC
Yes, the web server is a full blown website in it's own right. Being this, you may want to add new controllers and views to the site. We'll start by making a controller named "Test". This will make it so that you can go to localhost:15942/test and have your own custom view processed and shown.
public class Test : PageController
{
public Test(NetWorker socket, ForgeWebServer webserver)
: base(socket, webserver)
{
// Key string used in html like this: <% print %>
variables.Add("print", Print);
}
public string Print()
{
return "HELLO TEST STRING ACTION!";
}
public override string Render()
{
return PresentHTML("test");
}
}As you can see from above, there are a lot of things going on, so we'll break them down one by one.
- You must derive your new controller from the
PageControllerclass - You must create the constructor for your controller with the supplied input arguments
- You should add any model pulling functions inside the constructor to the variables
- You should create methods that are going to return the data needed for the variables
- You must override the render method as it is abstract. This is where you return the HTML for your page. You can use the built in
PresentHTMLmethod you get from the parent class and pass in the name of your HTML page that will be presented.
When you add callbacks assigned to a string to variables as seen above with print, you are making it so that you can use the string response from those callbacks within your HTML page. See the test.html example below to see how it is used in the <% %> tags.
Now that you have your new controller, you are probably curious on how you can get it to be used while the web server is running. Below you will notice the line that has TryAddController which will try to add your new controller to the web server. Notice that all we have to do is new up the controller and pass it into this method.
ForgeWebServer ws = new ForgeWebServer(server, 15942, dict);
ws.Start();
ws.TryAddController(new Test(server, ws));Finally, we are ready to create our view. A view is something that the controller will present to the user when they request your controllers web page. We will name this file test.html because we are passing test into the PresentHTML of our controller. If we were to name this html file cats then we would need to update the controller to pass "cats" into the PresentHTML method of the Render function.
<html>
<head>
<title>My Test Page</title>
</head>
<body>
<h1>Hello <% print %></h1>
</body>
</html>Note if you are not sure what the HTML page is, be sure to check out the Jump Start Guide
That's it, you have your fancy new controller and view and you can start using it immediately inside of your web server.
Getting Started
Network Contract Wizard (NCW)
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
Master Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
-
Connection Cycle Events
-
Rewinding
-
Network Logging
-
Working with Multiple Sockets
-
Modify Master and Standalone servers
-
NAT Hole Punching
-
UDP LAN Discovery
-
Offline Mode
-
Ping Pong
-
Lobby System
-
Upgrading Forge Remastered to Develop branch or different version
-
Forge Networking Classic to Remastered Migration Guide
-
Script to easily use Forge Networking from sources
-
Run Two Unity Instances with Shared Assets for Easiest Dedicated Client Workflow