Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Unity & API Gateway - How to send a websocket action?

Discussion in 'Multiplayer' started by CostasV, Apr 28, 2020.

  1. CostasV

    CostasV

    Joined:
    Mar 30, 2020
    Posts:
    2
    HI all, first post!

    I am building a little multiplayer game using all serverless AWS services in the backend (API Gateway, Lambda, DynamoDB, etc.). I’m not a software engineer by training, so I hit roadblocks more often than I'd like to admit.

    I have encountered problems with trying to send actions to AWS API Gateway using a Websocket connection. Whenever I attempt to send a json string, it comes out the other end with the \" backouts (e.g. "body": "{\"action\":\"get_player_status\",\"username\":\"asdasd\"}"). API Gateway is unable to parse this and always calls the $default action. I have tried different approaches to combat this, but without success. Right now I have a Lambda Python function that takes all messages, corrects the json string, then forwards it to the correct Lambda function. This is basically doing what API Gateway is supposed to do, but with much higher latency and cost.

    Any ideas?

    Code snippet:

    Code (CSharp):
    1.  
    2. using NativeWebSocket;
    3.  
    4. [Serializable]
    5. public class SendAction
    6. {
    7.     public string action;
    8.     public string username;
    9. }
    10.  
    11.  
    12. void GetPlayerStatus()
    13. {
    14.     SendAction getStatus = new SendAction();
    15.     getStatus.action = "get_player_status";
    16.     getStatus.username = username;
    17.  
    18.     string jsonGetStatus = JsonUtility.ToJson(getStatus);
    19.  
    20.     SendWebSocketMessage(jsonGetStatus);
    21. }
     
  2. CostasV

    CostasV

    Joined:
    Mar 30, 2020
    Posts:
    2
    Solved by using Newtonsoft.json to hand it.

    string json = JsonConvert.SerializeObject(body, Formatting.Indented);