Reputation: 81
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
}
Upvotes: 1
Views: 1124
Reputation: 53
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);
});
Upvotes: 1