ProtonNet Server homepage

GitHub release License GitHub star chart

I. Introduction

Proton Net Server is a comprehensive solution for building multiplayer server systems based on the .NET platform and the C# programming language. This solution works seamlessly with Unity and supports deployment across multiple platforms.

II. Supported IDEs

III. Key Features

IV. Quick Start

1. Setting Up the Server

Installing ProtonNet Template for Visual Studio Code

Creating a Server Project

For example, let’s create a project called SocketServerExample with ProtonNet version 1.0.4 and targeting .NET 8.0:

Create a SocketServer project

Once created, you can run the project by selecting Launch SocketServerExample.Startup (Debug) in Visual Studio Code:

Running the application

Adding a New Handler

Suppose you want to add a login command. Navigate to the Handlers/RequestHandlers folder and add the file LoginRequestHandler.cs:

Add a RequestHandler

Edit the content of LoginRequestHandler.cs as follows:

class LoginRequestModel
{
    [StringDataMember(Code = "username", MinLength = 6, MaxLength = 20)]
    public string Username;

    [StringDataMember(Code = "password", MinLength = 6, MaxLength = 20)]
    public string Password;
}

[AllowAnonymous]
class LoginRequestHandler : RequestHandler<LoginRequestModel>
{
    public override string GetOperationCode() => "login";

    private IDictionary<string, string> userRepo { get; }

    public override async Task<OperationResponse> Handle(LoginRequestModel requestModel, OperationRequest operationRequest, SendParameters sendParameters, IUserPeer userPeer, ISession session)
    {
        this.logger.Info("Received request: " + requestModel.Username + " " + requestModel.Password);

        if (this.userRepo.TryGetValue(requestModel.Username, out var password))
        {
            if (password == requestModel.Password)
            {
                return new OperationResponse(operationRequest.OperationCode)
                {
                    DebugMessage = "Login success",
                    ReturnCode = ReturnCode.Ok
                };
            }
        }

        return new OperationResponse(operationRequest.OperationCode)
        {
            DebugMessage = "Invalid username or password",
            ReturnCode = ReturnCode.OperationInvalid
        };
    }

    public LoginRequestHandler()
    {
        this.userRepo = new Dictionary<string, string>
        {
            ["admin"] = "123456"
        };
    }
}

Run the project again and test it.

2. Unity Client

Download and Import Unity Package

Create a MonoBehaviour Script

Create a new MonoBehaviour script in Unity with the following content:

public class ProtonNetworkBehaviour : MonoBehaviour {
    IClientPeerFactory clientPeerFactory;
    ISocketClientPeer socketClientPeer;

    void Start()
    {
        LogManager.SetLoggerFactory(UnityLoggerFactory.Instance);

        clientPeerFactory = UnityClientPeerFactory.NewBuilder()
            .SetAutoCallService(true)
            .Build();

        socketClientPeer = clientPeerFactory.NewSocketClientPeer("http://127.0.0.1:32202", XmobiTea.ProtonNet.Client.Socket.Types.TransportProtocol.Tcp);
        socketClientPeer.Connect(true, (connectionId, serverSessionId) =>
        {
            Debug.Log("OnConnected");
            Debug.LogError(connectionId + " " + serverSessionId);
        }, (reason, message) =>
        {
            Debug.Log("OnDisconnected");
            Debug.LogError(reason + " " + message);
        });
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            socketClientPeer.Send(new OperationRequest()
                .SetOperationCode("login")
                .SetParameters(GNHashtable.NewBuilder()
                    .Add("username", "admin")
                    .Add("password", "123456")
                    .Build()), response =>
                {
                    Debug.Log("Received from Server: " + response.ReturnCode + ", DebugMessage: " + response.DebugMessage);
                }, new SendParameters()
                {
                    Encrypted = false,
                });
        }
    }
}

V. Additional Information

VI. Documentation and Examples

ProtonNet Documentation

For more detailed examples, check out ProtonNet Examples.

VII. Deploy production

Use ProtonNet Control to deploy your solution for production.

VIII. Need Help?

Enjoy your development with ProtonNet!