Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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));