简体   繁体   English

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

[英]$_POST and WWWForm with bool.ToString() doesn't work

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. 我的php脚本将命令输出为文本,然后变得统一起来将其转换为c#命令。

Here's the ServerCalls.cs: 这是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('\n');
        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: 对于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\n";
    if ($isAutoConnectToLobby == true){
        echo "OnConnectedToLobby\n";
    }
    if($isShowingLobbyGui == true){
        echo "showLobbyGui\n";
    }
    if($ClientClickConnectButton == true){
        sleep(5);      //Just to give some time to complete any in-complete operations
        echo "onClientClickConnectButton\n";
    }
    if($hasPlayerJoinedRoom == true){
        echo "OnPlayerJoinedRoom\n";
    }

?>

The problem is that the ServerMain.php script doesn't seem to listen to any of the WWWForm fields. 问题在于ServerMain.php脚本似乎没有侦听任何WWWForm字段。 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 如果在php脚本中的if语句中添加引号,则只会遍历所有if语句,但如果不添加引号,则即使启用了公共布尔,它也不会执行任何if语句

使用isset()首先测试该值是否可用;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM