简体   繁体   中英

Socket.IO in unity5 ,C# - how to emit string or object?

I'm new to C# and unity. I'm trying to send a simple string to the server using Socket.IO:

socket.Emit("collision", "Surface") It doesn't work, i get:

如何通过套接字io统一发送/发送字符串?

I tried a bunch of things, the following code works only with strings as numbers:

var dataToSend = new JSONObject();
dataToSend["colider"] = new JSONObject("2"); // this works..
socket.Emit("collision", dataToSend);

the following gives null - third one in the image below..

var dataToSend = new JSONObject();
dataToSend["colider"] = new JSONObject("Surface");
socket.Emit("collision", dataToSend);

Thig gives empty object { }

var dataToSend = new JSONObject();
dataToSend["colider"] = new JSONObject(JsonUtility.ToJson("Surface"));
socket.Emit("collision", dataToSend);

I get:

适用于数字作为字符串,但不适用于字符串作为string.png

So how can i send/emit this "Surface" string to the server? Why is it so hard, it's just a simple string .. ?! thank you:)

I found it: socket.Emit("collision", JSONObject.CreateStringObject("Surface"));

There are a bunch of more specialized functions - the API can be found inside SocketIO/JSONObject/JSONObject.cs - for the Socket.IO library downloaded from assets store

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM