PUN Classic (v1), PUN 2 and Bolt are in maintenance mode. PUN 2 will support Unity 2019 to 2022, but no new features will be added. Of course all your PUN & Bolt projects will continue to work and run with the known performance in the future. For any upcoming or new projects: please switch to Photon Fusion or Quantum.

EnableLanBroadcast & Example

The Lan Broadcast procedure is a interesting way to publish game sessions only on your LAN, making it possible to match players that are on the same network.
The session broadcast is most useful if you want to join players locally, but does not want to share the game info publically or if you are using the Pro version, don't need to type the server IP to connect, for example.

In order to publish the game session using the built in broadcaster and listen for sessions that comes from any active server, you only need to call the utility method BoltNetwork.EnableLanBroadcast() (API link). It will initialize the internal broadcaster, so you just need to keep the server running or check for sessions on the client.

Photon Bolt Free

On the code below, using Photon Bolt Free, it shows how you can create a Photon session privately and publish it via LAN. This is done by making an invisible room and enabling the LAN broadcast.
There are two main aspects on this case: (i) even if the room is invisible, any client with its ID can join the session normally, including clients outside of your LAN, as the session is always published to the Cloud service, and (ii) as you are using the Free version, an internet connection is always required, even if the players are only local.

The script shows a simple Menu UI with buttons to start the peer as the Server or Client.
Once started, the server will create a new Photon Session, observe that we set it as invisible (props.IsVisible = false;), and publish it to the Photon Cloud.
In both cases, it's invoked the BoltNetwork.EnableLanBroadcast() at the end of BoltStartDone(), which will trigger the session broadcaster.

Eventually, you will see the client sending broadcast searches to the broadcast port of your router.
If you have an active server running on the same LAN, that server should respond to the search with its own session information.
The UI will be populated with a Join button, that initiates the connection with the game server.
At this moment, the connection occurs in the exact same way as when you join a public Photon Session.
So, once again, you need to be connected to the internet in order to enter the room.

C#

using System;
using Bolt.Matchmaking;
using Bolt.Photon;
using UdpKit;
using UdpKit.Platform;
using UnityEngine;

public class MenuLanBroadcast : Bolt.GlobalEventListener
{
    private Rect _labelRoom = new Rect(0, 0, 140, 75);
    private GUIStyle _labelRoomStyle;

    private void Awake()
    {
        Application.targetFrameRate = 60;
        BoltLauncher.SetUdpPlatform(new PhotonPlatform());

        _labelRoomStyle = new GUIStyle()
        {
            fontSize = 20,
            fontStyle = FontStyle.Bold,
            normal =
                {
                    textColor = Color.white
                }
        };
    }

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

    public override void BoltStartDone()
    {
        if (BoltNetwork.IsServer)
        {
            string matchName = Guid.NewGuid().ToString();

            var props = new PhotonRoomProperties();

            props.IsOpen = true;
            props.IsVisible = false; // Make the session invisible

            props["type"] = "game01";
            props["map"] = "Tutorial1";

            BoltMatchmaking.CreateSession(
                sessionID: matchName,
                sceneToLoad: "Tutorial1",
                token: props
            );
        }

        // Broadcast and Listen for LAN Sessions
        BoltNetwork.EnableLanBroadcast();
    }

    public override void SessionListUpdated(Map<Guid, UdpSession> sessionList)
    {
        BoltLog.Info("Session list updated: {0} total sessions", sessionList.Count);
    }

    // GUI

    public void OnGUI()
    {
        GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));

        if (BoltNetwork.IsRunning == false)
        {
            if (ExpandButton("Start Server"))
            {
                BoltLauncher.StartServer();
            }

            if (ExpandButton("Start Client"))
            {
                BoltLauncher.StartClient();
            }
        }
        else if (BoltNetwork.IsClient)
        {
            SelectRoom();
        }

        GUILayout.EndArea();
    }

    private void SelectRoom()
    {
        GUI.Label(_labelRoom, "Looking for rooms:", _labelRoomStyle);

        if (BoltNetwork.SessionList.Count > 0)
        {
            GUILayout.BeginVertical();
            GUILayout.Space(30);

            foreach (var session in BoltNetwork.SessionList)
            {
                UdpSession udpSession = session.Value;

                var label = string.Format("Join: {0} | {1}", udpSession.HostName, udpSession.Source);

                if (ExpandButton(label))
                {
                    BoltMatchmaking.JoinSession(udpSession);
                }
            }

            GUILayout.EndVertical();
        }
    }

    private bool ExpandButton(string text)
    {
        return GUILayout.Button(text, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
    }
}

Photon Bolt Pro

It's also possible to accomplish a similar behavior using Photon Bolt Pro.
Below you can see a very similar script like the one shown earlier, but this time using the direct connection capabilities of the Pro version.
In this case, you will also listen to Game Session data, but they will be available only on LAN.
We've removed most of the duplicated code and highlight only the main changes, the rest of the script is the same.

C#

public class MenuLanBroadcastPro : Bolt.GlobalEventListener {

    private void Awake()
    {
        //...
        // Change the target Udp Platform
        BoltLauncher.SetUdpPlatform(new DotNetPlatform());
        // ...
    }

    public override void BoltStartDone()
    {
        if (BoltNetwork.IsServer)
        {
            // There is no need to setup the properties of the Game Session
            // just create one normally

            string matchName = Guid.NewGuid().ToString();

            BoltMatchmaking.CreateSession(
                sessionID: matchName,
                sceneToLoad: "Tutorial1"
            );
        }

        // Broadcast and Listen for LAN Sessions
        BoltNetwork.EnableLanBroadcast();
    }

    // ...
}

Extra Notes

If you are working with Android devices, by default the OS will not let you send and listen for broadcast messages, so you need to make sure to add this permission to the Android manifest file: <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">.

Back to top