问题描述
我在官方论坛上问了这个问题,但我猜我的代表那里没有足够的人来认真对待:O
I asked this question on the official forum but I guess I don't have enough rep there to be taken seriously :O
我免费使用Unity3D.
I'm using Unity3D free.
有人使用过 https://www.assetstore.unity3d.com/zh/#!/content/21721 成功吗?这个插件实际上是最接近工作的(我也尝试过此和此,但它们对我不起作用.
Has anyone used https://www.assetstore.unity3d.com/en/#!/content/21721 with success? This plugin actually came the closest to working (I also tried this and this but they don't work for me).
我联系了作者,但还没有得到答复,所以想知道是否有人做过这项工作吗?
I contacted the author but haven't got a reply yet, so was wondering if someone had made this work?
(我想指出的是,如果您使用过其他插件,并且发现通过SocketIO与Node.js服务器进行通讯很容易/有用,那么我不介意购买其他插件-因此请推荐)
具体来说,这是我的问题:我找不到从Node.js向JSON发送JSON数据的方法,因为它不断出错.
Concretely, here's my problem with it:I cant find a way to send JSON data to Unity from Node.js as it keeps getting an error.
我尝试了很多方法,这就是其中一种:
I tried numerous ways, and this is one of them:
io.on('connection', function(socket){
console.log('a user connected');
var data ='{"email":"[email protected]","pass":"1234"}';
var dataJson = JSON.stringify(data);
console.dir(dataJson);
socket.emit('newResults', dataJson);
console.log('server emited newResults');
socket.on('fromClient', function(data){
console.log("got msg from client");
console.dir(data);
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
在Unity3D中,我使用以下函数对此进行拦截:
In Unity3D I use the following function to intercept this:
public void HandleNewResults(SocketIOEvent e){
Debug.Log(string.Format("[name: {0}, data: {1}]", e.name, e.data));
Debug.Log (new JSONObject (e.data));
}
但此时(打开调试时)此消息使它崩溃(捕获错误信号):
but it crashes (it catches the error signal) at this point with (when debugging is turned on) this message:
SocketComm:TestError(SocketIOEvent) (at Assets/_Scripts/SocketComm.cs:58)
SocketIO.SocketIOComponent:EmitEvent(SocketIOEvent) (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:400)
SocketIO.SocketIOComponent:EmitEvent(String) (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:392)
SocketIO.SocketIOComponent:OnError(Object, ErrorEventArgs) (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:382)
WebSocketSharp.Ext:Emit(EventHandler`1, Object, ErrorEventArgs) (at Assets/SocketIO/WebsocketSharp/Ext.cs:992)
WebSocketSharp.WebSocket:error(String) (at Assets/SocketIO/WebsocketSharp/WebSocket.cs:1011)
WebSocketSharp.WebSocket:Send(String) (at Assets/SocketIO/WebsocketSharp/WebSocket.cs:1912)
SocketIO.SocketIOComponent:EmitPacket(Packet) (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:309)
SocketIO.SocketIOComponent:EmitClose() (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:299)
SocketIO.SocketIOComponent:Close() (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:184)
SocketIO.SocketIOComponent:OnApplicationQuit() (at Assets/SocketIO/Scripts/SocketIO/SocketIOComponent.cs:164)
能否请您阐明如何解决此问题?
Can you please shed some light on how to aproach this problem?
推荐答案
但是SocketIO需要Unity Pro.如果要使用本机.NET套接字.
But SocketIO needs Unity Pro. If you want to use native .NET sockets.
您可以使用好的ol'套接字.它可用于插座.
You can use Good ol' Sockets.It's usable for sockets.
这篇关于从Node.js发送JSON数据到Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!