1

I have this string in JSON format (C#):

coordinatesJson = "{\"latitude\":\"" + latitude + "\",\"longitude\":\"" + longitude + "\"}";

That I send to my Node.JS server using Socket.IO as such:

socket.Emit("send coordinates", JSONObject.CreateStringObject(coordinatesJson));

On my server I have:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);

app.get('/', function(req, res){
res.send('Connection Successful');
});

io.on('connection', function(socket){

socket.on('client connect', function(){
    console.log("player connected");
    socket.emit("test");

});

socket.on('send coordinates', function(data){
    console.log(data);
    var data = JSON.parse(data);
    console.log(data);
    });
});

console.log('\n--- Server is running...\n');

But nothing appears. Any help?

EDIT 1: I meant if I do:

coordinatesJson = "{ 'latitude': '"+latitude+"', 'longitude': '"+longitude+"' }";

This reaches the server and outputs the proper string

Edit 2: C# Code:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using SocketIO;
using UnityEngine.UI;

public class NetworkManager : MonoBehaviour {

public SocketIOComponent socket;
public Text serverMessageDebug;

private string latitude;
private string longitude;
private string coordinatesJson;

void Start() {

    latitude = "5"; //GPS.Instance.latitude.ToString();
    longitude = "5"; //GPS.Instance.longitude.ToString();

    //coordinatesJson = "{ 'latitude': '"+latitude+"', 'longitude': '"+longitude+"' }";
    coordinatesJson = "{\"latitude\":\"" + latitude + "\",\"longitude\":\"" + longitude + "\"}";
    Debug.Log(coordinatesJson);


    EstablishConnection ();
    //Subscrible to node.js websocket events
    socket.On ("test", OnTest);

}

#region Server Connection

public void EstablishConnection(){      
    StartCoroutine (ConnectToServer ());
}

private IEnumerator ConnectToServer(){      
    yield return new WaitForSeconds (0.5f);
    socket.Emit("client connect");
    socket.Emit("send coordinates", JSONObject.CreateStringObject(coordinatesJson));
    yield return new WaitForSeconds (1f);
}

#endregion
#region Websocket Events
private void OnTest(SocketIOEvent socketIOEvent){
    Debug.Log ("This is a test from the server");
    serverMessageDebug.text = "This is a test from the server";
    }
#endregion
}
11
  • "nothing", or null or '', or an error?. Put a console.log before JSON.parse, does it reach there?, If it does, show what's data. Commented Apr 10, 2018 at 22:53
  • Nothing, as if the line was never executed. If I write the string using a combination of single quotes and double quotes as such: "{ 'latitude': '"+latitude+"', 'longitude': '"+longitude+"' }"; I actually comes through, but then when I try to parse it, it complains about the single quotes. I guess JSON isn't fond of single quotes? Commented Apr 10, 2018 at 22:54
  • Show server side code, for all we know you don't have a listener. Commented Apr 10, 2018 at 22:55
  • you can use backticks (`), but it's readable. When we see how are you setting up socket.io, and if it's even listening or running we can help you :) Commented Apr 10, 2018 at 23:00
  • So, you're saying that if you send the raw string, without JSONObject... it reaches the server? Commented Apr 10, 2018 at 23:01

1 Answer 1

1

this should work. first create a class like this:

[Serializable]
public class CoordinatesJSON
{
    public double[] Coordinates;

    public CoordinatesJSON(double latitude, double longitude)
    {
        Coordinates = new double[] { latitude, longitude };
    }
}

then make a jsonObject method like this:

JSONObject ConvertDataToJson()
{
    double lat = GPS.latitude;
    double lon = GPS.longitude;

    string data = JsonUtility.ToJson(new CoordinatesJSON(lat, lon));
    return new JSONObject(data);
}

and send data to Server like this:

public void SendGPSCoordinateToServer()
{
    
    socket.Emit("send coordinates", ConvertDataToJson());
}

and serve side:

socket.on('send coordinates', function(data){

    console.log('Json Data: '+JSON.stringify(data));
    console.log('Data: ' + data.Coordinates);

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.