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
}
nullor'', or an error?. Put aconsole.logbeforeJSON.parse, does it reach there?, If it does, show what's data.JSONObject...it reaches the server?