dqlm80253 2015-10-26 21:09
浏览 54

使用bool.ToString()的$ _POST和WWWForm不起作用

I have been working on making a custom server (probably won't work but I want to give it a try and work on it as a hobby) but I've already run into a problem! What I have the php scripts outputing the commands as text then getting unity to translate that into the c# commands.

Here's the ServerCalls.cs:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Linq;

public class ServerCallsScript : MonoBehaviour {

    private bool isPlayerInRoom = false;

    public bool autoConnectToLobby;
    public bool showLobbyGUI;

    public float width;
    public float height;
    public float x;
    public float y;

    public enum logTypeEnum {
        Developer,
        Debug,
        Warning,
        Error
    };
    public logTypeEnum logType = logTypeEnum.Warning;

    public string MainServerUrl = "https://network-hlapi-myusername.c9.io/serverMain.php";

    private string serverText = "";

    void Start()
    {
        StartCoroutine(ServerCommands());
    }
    void Update()
    {

    }
    void OnGUI()
    {
        if (showLobbyGUI)
        {
            if(GUI.Button(new Rect(5,80,115,21), "Create Game"))
            {

            }
            GUI.TextField(new Rect(130, 80, 115, 21), "Game1");
            if(GUI.Button(new Rect(5, 110, 115, 21), "Join Game"))
            {

            }
            GUI.TextField(new Rect(130, 110, 115, 21),"Game Name");
        }
    }



    /// <summary>
    /// This is called as soon as the player opens the scene
    /// </summary>
    public void OnConnectedToServer()
    {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Successfully Connected To Server at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }
    /// <summary>
    /// Called When Player Connects To Lobby. Automatically called if auto-join lobby is enabled
    /// </summary>
    public void OnConnectedToLobby() {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Connected To Lobby at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }
    public void ShowLobbyGui() {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Showing Lobby Gui");
        }

    }
    /// <summary>
    /// Called when the player joins a game
    /// </summary>
    public void OnPlayerJoinedRoom()
    {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Player Successfully Joined Room at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }



    IEnumerator ServerCommands() {
        Debug.Log("Collecting Info from Server...");
        WWWForm Mainform = new WWWForm();
        Mainform.AddField("isAutoJoinLobby", autoConnectToLobby.ToString());
        Mainform.AddField("isShowingLobbyGui", showLobbyGUI.ToString());
        Mainform.AddField("hasPlayerJoinedRoom", isPlayerInRoom.ToString());

        WWW mainServer = new WWW(MainServerUrl, Mainform);
        yield return mainServer;


        serverText = mainServer.text;
        string[] serverCommands = serverText.Split('
');
        if(mainServer.error != null)
        {
            Debug.LogError("We encountered an error! Error:" + mainServer.error);
        }
        if (serverCommands.Contains("OnConnectedToServer"))
        {
            OnConnectedToServer();
        }
        if (serverCommands.Contains("OnConnectedToLobby"))
        {
            Debug.Log("OnConnectedToLobby");
            OnConnectedToLobby();
        }
        if (serverCommands.Contains("showLobbyGui"))
        {
            ShowLobbyGui();
        }
        if (serverCommands.Contains("OnPlayerJoinedRoom"))
        {
            OnPlayerJoinedRoom();
        }

    }
}

For ServerMain.php:

<?php
    $servername = getenv('IP');
    $username = getenv('C9_USER');
    $password = "I Wouldn't Tell You That Now";
    $database = "server";
    $dbport = 3306;

    // Create connection
    $db = mysql_connect($servername, $username, $password, $dbport)or die("Cant Connect to server");
    mysql_select_db($database) or die("Cant connect to database");

    $isAutoConnectToLobby = $_POST['isAutoJoinLobby'];
    $isShowingLobbyGui = $_POST['isShowingLobbyGui'];
    $hasPlayerJoinedRoom = $_POST['hasPlayerJoinedRoom'];
    $ClientClickConnectButton = $_POST['ClientClickConnectButton'];

    echo "OnConnectedToServer
";
    if ($isAutoConnectToLobby == true){
        echo "OnConnectedToLobby
";
    }
    if($isShowingLobbyGui == true){
        echo "showLobbyGui
";
    }
    if($ClientClickConnectButton == true){
        sleep(5);      //Just to give some time to complete any in-complete operations
        echo "onClientClickConnectButton
";
    }
    if($hasPlayerJoinedRoom == true){
        echo "OnPlayerJoinedRoom
";
    }

?>

The problem is that the ServerMain.php script doesn't seem to listen to any of the WWWForm fields. If you add quotes to the if statements in the php script it just goes through all the if statements but if you don't add quotes it doesn't do any of the if statements, even if the public bools are enabled

  • 写回答

1条回答

  • dongpu1908 2015-10-28 11:07
    关注

    Use isset() to first test if the value is available ;-)

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug