Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    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));