Nikola
Nikola

Reputation: 15038

send JSON data to Unity from Node.js

I asked this question on the official forum but I guess I don't have enough rep there to be taken seriously :O

I'm using Unity3D free.

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?

(edit: I would like to point out that I don't mind buying some other plugin if you have used it and found it easy/useful to communicate with your Node.js server via SocketIO - so please recommend)

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');
    });
});

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?

Upvotes: 0

Views: 4283

Answers (3)

Turkm
Turkm

Reputation: 3

Make these changes:

    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);
    console.log('server emited newResults');

    socket.on('beep', function(){
        socket.emit('boop', {'boop': dataJson});
        console.log("got msg from client");
        console.dir(data);
    });
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});

this is will work.

Upvotes: 0

BlackRainbow
BlackRainbow

Reputation: 1904

I used such code:

socket.emit('event_name', {"email":"[email protected]","pass":"1234"});

I do not know if it right. The function in unity is:

public void TestBoop (SocketIOEvent e)
        {
                Debug.Log ("[SocketIO] Boop received: " + e.name + "--" + e.data);
                );
        }

And output is:

[SocketIO] Boop received: boop--{"email":"[email protected]","pass":"1234"}

Perhaps it does't right at all but the result at least comes to unity

Upvotes: 0

Barış Çırıka
Barış Çırıka

Reputation: 1570

I'm using Unity3D free.

But SocketIO needs Unity Pro. If you want to use native .NET sockets.

Unity Pro is required in order to build using .NET sockets. You may use a replacement package if you don't want to use native sockets.

You can use Good ol' Sockets.It's usable for sockets.

Upvotes: 1

Related Questions