[英]Sending JSON string to Node.JS server using Socket.IO returning nothing
I have this string in JSON format (C#):我有这个 JSON 格式的字符串(C#):
coordinatesJson = "{\"latitude\":\"" + latitude + "\",\"longitude\":\"" + longitude + "\"}";
That I send to my Node.JS server using Socket.IO as such:我使用 Socket.IO 发送到我的 Node.JS 服务器,如下所示:
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:编辑 1:我的意思是如果我这样做:
coordinatesJson = "{ 'latitude': '"+latitude+"', 'longitude': '"+longitude+"' }";
This reaches the server and outputs the proper string这到达服务器并输出正确的字符串
Edit 2: C# Code:编辑 2:C# 代码:
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
}
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 方法,如下所示:
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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.