IProtocolToken

Just a brief simple example of using the new IProtocolToken functionality. Set bolt to require manual acceptance of connections.

First, use this script for your menu scene, replacing BoltInit.cs

using UnityEngine;
using System.Collections;
using System;
using UdpKit;
using UnityEngine.SceneManagement;


public class BoltInitToken : Bolt.GlobalEventListener
{
    enum State
    {
        SelectMode,
        SelectMap,
        EnterServerIp,
        StartServer,
        StartClient,
        Started,
    }

    State state;

    string map;
    string serverAddress = "127.0.0.1";
    string name = "name";

    int serverPort = 25000;

    void Awake()
    {
        serverPort = BoltRuntimeSettings.instance.debugStartPort;
    }

    void OnGUI()
    {
        Rect tex = new Rect(10, 10, 140, 75);
        Rect area = new Rect(10, 90, Screen.width - 20, Screen.height - 100);

        GUI.Box(tex, Resources.Load("BoltLogo") as Texture2D);
        GUILayout.BeginArea(area);

        switch (state)
        {
            case State.SelectMode: State_SelectMode(); break;
            case State.SelectMap: State_SelectMap(); break;
            case State.EnterServerIp: State_EnterServerIp(); break;
            case State.StartClient: State_StartClient(); break;
            case State.StartServer: State_StartServer(); break;
        }

        GUILayout.EndArea();
    }

    private void State_EnterServerIp()
    {
        GUILayout.BeginHorizontal();

        GUILayout.Label("Server IP: ");
        serverAddress = GUILayout.TextField(serverAddress);
        name = GUILayout.TextField(name); ;
        if (GUILayout.Button("Connect"))
        {
            state = State.StartClient;
        }

        GUILayout.EndHorizontal();
    }


    void State_SelectMode()
    {
        if (ExpandButton("Server"))
        {
            state = State.SelectMap;
        }
        if (ExpandButton("Client"))
        {
            state = State.EnterServerIp;
        }
    }

    void State_SelectMap()
    {
        foreach (string value in BoltScenes.AllScenes)
        {


            if (SceneManager.GetActiveScene().name != value)
            {
                if (ExpandButton(value))
                {
                    map = value;
                    state = State.StartServer;
                }
            }
        }
    }

    void State_StartServer()
    {
        BoltLauncher.StartServer(new UdpEndPoint(UdpIPv4Address.Any, (ushort)serverPort));

        state = State.Started;
    }

    void State_StartClient()
    {
        BoltLauncher.StartClient(UdpEndPoint.Any);
        state = State.Started;
    }

    bool ExpandButton(string text)
    {
        return GUILayout.Button(text, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
    }
    public override void BoltStartDone()
    {
        if (BoltNetwork.isClient)
        {
            UserToken userToken = new UserToken(name);

            BoltNetwork.Connect(new UdpEndPoint(UdpIPv4Address.Parse(serverAddress), (ushort)serverPort), userToken);

        }
        else
        {
            BoltNetwork.LoadScene(map);

        }
    }

}

This class is for whatever data you want to send from the client to the server when opening a connection.

  using UnityEngine;
using UdpKit;
using Bolt;

public class UserToken : IProtocolToken
{

    public string username;

    public UserToken(string username)
    {
        this.username = username;
    }

    public UserToken()
    {
        this.username = "";
    }

    public void Write(UdpPacket packet)
    {
        packet.WriteString(username);
    }

    public void Read(UdpPacket packet)
    {
        username = packet.ReadString();
    }
}

This class will set your BoltConnection.AcceptToken and choose whether to accept or refuse the connection request.

  using System;
using Bolt;
using UdpKit;

public class LocalUserToken : IProtocolToken
{
    public string username;
    public string connectionTime;

    public LocalUserToken()
    {
        this.username = "";
        this.connectionTime = "";
    }

    public LocalUserToken(UserToken userToken, string connectionTime)
    {
        this.username = userToken.username;
        this.connectionTime = connectionTime;
    }

    public void Read(UdpPacket packet)
    {
        packet.WriteString(username);
        packet.WriteString(connectionTime);
    }

    public void Write(UdpPacket packet)
    {
        username = packet.ReadString();
        connectionTime = packet.ReadString();
    }
}


[BoltGlobalBehaviour]
public class TokenCallbacks : Bolt.GlobalEventListener
{

    public override void BoltStartBegin()
    {
        BoltNetwork.RegisterTokenClass<UserToken>();
        BoltNetwork.RegisterTokenClass<LocalUserToken>();
    }

    //requires manual accept mode in Bolt Settings
    public override void ConnectRequest(UdpEndPoint endpoint, IProtocolToken token)
    {

        UserToken userToken = (UserToken)token;

        if (userToken.username.Equals("Dave"))
        {
            // Don't let Dave back on the server
            BoltNetwork.Refuse(endpoint);          
        }
        else
        {
            // Accept the connection and add a userToken to the connection
            BoltNetwork.Accept(endpoint, new LocalUserToken(userToken, System.DateTime.UtcNow.ToString()));
        }
    }
}

 To Document Top