Proper setup for self-hosted server?

Options
So I am trying to set up a self-hosted server for a project I am doing. Currently I have the settings in the Unity inspector as set to self-hosted, the server IP is set to my local IP and the port is set to 5055, the Appid is entered,and the RunInBackGround checkbox is checked as well as the AutoJoinLobby client setting checkbox. In the Photon Control I have the Game Server IP set to Autodetect public. On my network I have a static public IP facing toward the WAN and in my LAN I have set up a static local IP so the IP never changes for my computer on my local network or on the outside internet. I have set up the router to forward traffic from port 5055 to port 5057 all to my local IP address. (since the IP is static from the ISP the IP really lives on the router and you don't forward to a public IP but to a local one) The firewall is configured to let traffic through for those ports as well. I am using the Unity SDK version of Photon Server which has two folders that are named MasterServer and GameServer, in both of these folders I opened the Photon.LoadBalancing.dll.config files and modified the value for the MasterIPAddress so that is reads to my static public IP. In the MasterServer file this was line 147 and in the GameServer file it was line 141. Now I have a script that is attached to a gameobject in my project that persists from the start to the main scene. I am currently using the ConnectWithSettings, and the JoinOrCreateRoom. My problem is that when I load into the main scene I can tell that OnJoinRoom is not being called because i have several prefabs that are supposed to instantiate when you load into the room. Below is my code and the console from when you load into the main scene. Any help with this would be very much appreciated.

Best Answer

  • NovaVR
    NovaVR
    Answer ✓
    Options
    So I got it working here is my configuration. MasterServer machine hosts the Photon Masterserver but cannot be the client at the same time. The Photon Server IP should be set to the local IP. The Photon.LoadBalancing.dll.config file should have the MasterIPAddress set to the original value of 127.0.0.1 and the PublicIPAddress(line 151 and 157 in the two versions) set to the actual public IP address. This is for both the GameServer and MasterServer versions of this file. My router has the port forwarding set up so that It passes all incoming public traffic on ports 5055 and 5056 to the local IP of the MasterServer machine (I do this part because I am using a static public IP). For each client machine they have to be on a different network than the one the MasterServer is on and in the PhotonServerSettings in Unity the MasterServer address is set to the public static IP. As a note you cannot connect a client on the same network to the MasterServer because the MasterServer returns the public IP as the GameServer address, and a client on the same network cannot connect using its own network's public IP.

Answers

  • NovaVR
    NovaVR
    edited April 2017
    Options
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Photon;
    using Dissonance;

    public class Connect : Photon.PunBehaviour {

    public Dropdown genderDropDown;
    public InputField roomName;

    private string room;
    private int playerCount = 0;
    private PhotonView masterView;
    private string _textMessage = "";
    private readonly List _textLog = new List();
    private int _count;
    private RoomOptions roomOptions;
    private TypedLobby PGS;

    private string address = "NOT_SHOWING_IN_EXAMPLE";
    private int port = 5505;
    private string appID = "42aa30ee-f094-4aa1-bdd0-e74ed652592c";
    private string gameVersion = "v.1";

    //status indicator for connection
    // ReSharper disable once UnusedMember.Local (Justification: Used implicitly by unity)
    private void OnGUI()
    {
    GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());

    if (PhotonNetwork.isMasterClient)
    GUILayout.Label(PhotonNetwork.connectionStateDetailed + " (Master Client)");
    else
    GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());

    if (PhotonNetwork.room != null)
    GUILayout.Label("Room: " + PhotonNetwork.room.Name);

    GUILayout.Label(PhotonNetwork.NetworkStatisticsToString());

    GUILayout.Label("Photon Players:");
    foreach (var player in PhotonNetwork.playerList)
    {
    if (player.IsLocal)
    GUILayout.Label("> '" + player.ID + "' (Local)");
    else
    GUILayout.Label("> '" + player.ID + "'");
    }

    GUILayout.Label("Dissonance Players:");
    var comms = FindObjectOfType();
    if (comms == null)
    {
    while (comms == null)
    {
    return;
    }
    }
    if (comms.isActiveAndEnabled)
    {
    foreach (var player in comms.Players)
    {
    if (!player.IsConnected)
    continue;

    if (player.Name == comms.LocalPlayerName)
    GUILayout.Label("> '" + player.Name + "' (Local)");
    else
    GUILayout.Label("> '" + player.Name + "' Speaking:" + player.IsSpeaking);
    }

    GUILayout.Space(25);

    GUILayout.Label("Chat: ");
    _textMessage = GUILayout.TextField(_textMessage);
    if (GUILayout.Button("Send"))
    {
    comms.Text.Send("Global", _textMessage);
    _textMessage = "";
    }

    for (int i = 0; i < Mathf.Min(10, _textLog.Count); i++)
    {
    GUILayout.Label(_textLog[_textLog.Count - 1 - i]);
    }
    }
    }

    // Automatically connect to the acting server
    void Start()
    {
    //bool connectToMaster = PhotonNetwork.ConnectToMaster(address, port, appID, gameVersion);
    PhotonNetwork.networkingPeer.SentCountAllowance = 15;
    PhotonNetwork.ConnectUsingSettings("LEAVE_THIS_ALONE");
    PhotonNetwork.automaticallySyncScene = true;
    DontDestroyOnLoad(gameObject);
    Debug.Log("Connecting to server...");
    PhotonNetwork.NetworkStatisticsEnabled = true;
    //PhotonNetwork.JoinLobby(PGS);
    }

    //Automatic Photon Call
    public override void OnConnectedToPhoton()
    {
    Debug.Log("Connected to server");
    Debug.Log("Server IP: " + PhotonNetwork.ServerAddress);
    }

    public override void OnConnectionFail(DisconnectCause cause)
    {
    Debug.Log("Failed to connect to server");
    Debug.Log("Cause seems to be: " + cause);
    }

    //Called when either host or join button is pushed in the start scene
    public void JoinOrHostRoom()
    {
    room = roomName.text;
    if (room == "" || room == null)
    {
    room = "room1";
    }
    bool iscreated = PhotonNetwork.JoinOrCreateRoom(room,roomOptions,PGS);
    PhotonNetwork.LoadLevel("Main");
    PhotonNetwork.automaticallySyncScene = true;
    Debug.Log("Created Room");
    Debug.Log("iscreated:" + iscreated);
    }

    //Automatic Photon Call
    public override void OnJoinedLobby()
    {
    Debug.Log("Joined Lobby");
    }

    //Called when a room is created
    public override void OnCreatedRoom()
    {
    Debug.Log("Room Created: " + PhotonNetwork.room.Name);
    }

    //Called when a room is joined
    public override void OnJoinedRoom()
    {
    if (PhotonNetwork.room != null)
    {
    Debug.Log("Connected to room: " + PhotonNetwork.room.Name);
    playerCount += 1;
    }
    else
    {
    Debug.Log("Not in a room.");
    }

    Debug.Log("Player count is: " + playerCount);

    PlayerSpawn();

    var comms = FindObjectOfType();

    comms.Rooms.Join("Global");
    comms.Text.MessageReceived += (msg) => { _textLog.Add(msg.Sender + " Says > " + msg.Message); };


    }

    //Photon Read Automatic Fallback
    public void OnPhotonRandomJoinFailed()
    {
    room = roomName.text;
    if (room == "" || room == null)
    {
    room = "room1";
    }
    Debug.Log("Failed to find room, creating host");
    PhotonNetwork.automaticallySyncScene = true;
    PhotonNetwork.CreateRoom(room);
    PhotonNetwork.LoadLevel("Main");
    }

    //Spawn a player prefab based on the gender selected in the dropdown
    public void PlayerSpawn()
    {
    if (genderDropDown.value == 1)
    {
    PhotonNetwork.Instantiate("MaleVRPlayer", ViveManager.instance.head.transform.position, ViveManager.instance.head.transform.rotation, 0);
    PhotonNetwork.Instantiate("leftHand", ViveManager.instance.leftHand.transform.position, ViveManager.instance.leftHand.transform.rotation, 0);
    PhotonNetwork.Instantiate("rightHand", ViveManager.instance.rightHand.transform.position, ViveManager.instance.rightHand.transform.rotation, 0);
    }
    if (genderDropDown.value == 2)
    {
    PhotonNetwork.Instantiate("FemaleVRPlayer", ViveManager.instance.head.transform.position, ViveManager.instance.head.transform.rotation, 0);
    PhotonNetwork.Instantiate("leftHand", ViveManager.instance.leftHand.transform.position, ViveManager.instance.leftHand.transform.rotation, 0);
    PhotonNetwork.Instantiate("rightHand", ViveManager.instance.rightHand.transform.position, ViveManager.instance.rightHand.transform.rotation, 0);
    }
    if (genderDropDown.value == 0)
    {
    PhotonNetwork.Instantiate("MaleVRPlayer", ViveManager.instance.head.transform.position, ViveManager.instance.head.transform.rotation, 0);
    PhotonNetwork.Instantiate("leftHand", ViveManager.instance.leftHand.transform.position, ViveManager.instance.leftHand.transform.rotation, 0);
    PhotonNetwork.Instantiate("rightHand", ViveManager.instance.rightHand.transform.position, ViveManager.instance.rightHand.transform.rotation, 0);
    }
    }
    }
  • NovaVR
    Options
    OpenVR initialized!
    Connecting to server...(UnityEngine.Debug:Log(Object))
    Connected to server(UnityEngine.Debug:Log(Object))
    Server IP: 10.1.10.12:5055(UnityEngine.Debug:Log(Object))
    JoinedLobby(UnityEngine.Debug:Log(Object))
    CreatedRoom(UnityEngine.Debug:Log(Object))
    iscreated:True(UnityEngine.Debug:Log(Object))
  • NovaVR
    Options
    So I got it working here is my configuration. MasterServer machine hosts the Photon Masterserver but cannot be the client at the same time. The Photon Server IP should be set to the local IP. The Photon.LoadBalancing.dll.config file should have the MasterIPAddress set to the original value of 127.0.0.1 and the PublicIPAddress(line 151 and 157 in the two versions) set to the actual public IP address. This is for both the GameServer and MasterServer versions of this file. My router has the port forwarding set up so that It passes all incoming public traffic on ports 5055 and 5056 to the local IP of the MasterServer machine (I do this part because I am using a static public IP). For each client machine they have to be on a different network than the one the MasterServer is on and in the PhotonServerSettings in Unity the MasterServer address is set to the public static IP. As a note you cannot connect a client on the same network to the MasterServer because the MasterServer returns the public IP as the GameServer address, and a client on the same network cannot connect using its own network's public IP.
  • NovaVR
    NovaVR
    Answer ✓
    Options
    So I got it working here is my configuration. MasterServer machine hosts the Photon Masterserver but cannot be the client at the same time. The Photon Server IP should be set to the local IP. The Photon.LoadBalancing.dll.config file should have the MasterIPAddress set to the original value of 127.0.0.1 and the PublicIPAddress(line 151 and 157 in the two versions) set to the actual public IP address. This is for both the GameServer and MasterServer versions of this file. My router has the port forwarding set up so that It passes all incoming public traffic on ports 5055 and 5056 to the local IP of the MasterServer machine (I do this part because I am using a static public IP). For each client machine they have to be on a different network than the one the MasterServer is on and in the PhotonServerSettings in Unity the MasterServer address is set to the public static IP. As a note you cannot connect a client on the same network to the MasterServer because the MasterServer returns the public IP as the GameServer address, and a client on the same network cannot connect using its own network's public IP.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @NovaVR,

    Thank you for choosing Photon!

    Please do not post duplicate discussions or open multiple support channels at the same time.
    Thanks.

    Closing this one as duplicate of this.
This discussion has been closed.