Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Sending JSONObject from Unity to Node.JS server

Discussion in 'Scripting' started by Davdoc, Apr 10, 2018.

  1. Davdoc

    Davdoc

    Joined:
    Apr 6, 2015
    Posts:
    14
    I am trying to send some data to my Node.JS server using Socket.io. I have a little experience with JSON, but I am no expert. I am using socket.Emit(string ev, JSONObject data), but I have no idea how to form a JSONObject with the data I need to send. I have so far:
    Code (CSharp):
    1.        
    2. void Start () {
    3.  
    4.     latitude = GPS.Instance.latitude.ToString();
    5.     longitude = GPS.Instance.longitude.ToString();
    6.  
    7.     string coordinatesJson = "{ 'latitude': latitude, 'longitude': longitude }";
    8.  
    9.     socket.Emit("send coordinates", coordinatesJson);
    10. }
    11.  
    But this doesn't work because coordinatesJson is a string, of course. Could someone explain how I can create a JSONObject to send this data?
     
  2. TheVahidGR

    TheVahidGR

    Joined:
    Jun 23, 2018
    Posts:
    1
    Change to this:
    Code (CSharp):
    1. socket.Emit("send coordinates", new JSONObject(coordinatesJson));