duandu8892 2017-12-03 00:17
浏览 86

如何将我的Unity项目连接到ec2实例上的mysql服务器

I am trying to get data from my MySQL server to my Unity project. I tried to get the data like so:

public string awsurl = "ec2-174-129-82-141.compute-1.amazonaws.com/connect_to_server.php";
IEnumerator GetScores()
{
        print("get scores start");
        WWW aws_get = new WWW(awsurl);
        yield return aws_get;
        print("getscore here");
        if (aws_get.error != null)
        {
            print("There was an error getting aws: " + aws_get.error);
        }
        else
        {
            print(aws_get.text); // this is a GUIText that will display the scores in game.
        } 
}

I have a php script on my server to handle the interaction because I am having trouble using the MySqlConnection library for my unity project. Here is my php script:

    $address = "localhost"
$dbusername = "root";
$dbpassword = "root";
$db_name = "watshoes";
$db_conn = new mysqli($address, $dbusername, $dbpassword, $db_name);
    if(isset($_POST['username'])) $username = $_POST['username'];
    if(isset($_POST['user_id'])) $user_id = $_POST['user_id'];
    if(isset($password)) $password =$_POST['password'];

    $stmt = $db_conn->prepare("SELECT image FROM ImageText");
    // "s" means the database expects a string
    $stmt->bind_param("s", $user_id);
    if($stmt->execute())
    {
        /* bind result variables */
        $stmt->bind_result($image);
        /* fetch value */
        $stmt->fetch();
        echo $image;
    }
    else
    {
        echo "query failed";
    }
    $stmt->close();
    $db_conn->close();

But every time I run the code I get this error:There was an error getting aws: Failed to connect to ec2-174-129-82-141.compute-1.amazonaws.com port 80: Timed out

Thanks for looking at the code and please let me know if there is anything else I can do to help.

  • 写回答

1条回答 默认 最新

  • duanmaifu3428 2017-12-03 12:39
    关注

    You have to add a rule to open the port 80 using security groups (Inbound tab)

    How your rule should look:

    How should look your rule

    By default the port 80 is close in EC2 AWS instances.

    And the URL is malformed in Unity Add http:// to your server URL In editor will work ok without http:// but it will fail in android.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R