1
\$\begingroup\$

I'm new to exciting world of Unity, and I have problem establishing network connection using NetworkTransport API.

I'm using NodeJS as server. And I have some questions.

  1. Does NetworkTransport.Connect method awaits some response message from server?
  2. What is the encoding and message signature of Connect, Disconnect, Send and Receive methods?

My client's code so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Network : MonoBehaviour {

private int unreliableChannel;
private ConnectionConfig config;
private HostTopology topology;
private int hostId;
private int connectionId;
private byte error;

void Start () {
    NetworkTransport.Init();
    config = new ConnectionConfig();
    unreliableChannel = config.AddChannel(QosType.Unreliable);
    topology = new HostTopology(config, 1);
    hostId = NetworkTransport.AddHost(topology, 4444);
    connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", 3051, 0, out error);
    Debug.Log(connectionId);
}

void Update () {
    int recHostId;
    int connectionId;
    int channelId;
    byte[] recBuffer = new byte[1024];
    int bufferSize = 1024;
    int dataSize;
    byte error;
    NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    switch (recData)
    {
        case NetworkEventType.Nothing:
            break;
        case NetworkEventType.ConnectEvent:
            break;
        case NetworkEventType.DataEvent:
            Debug.Log("Message: " + System.Text.Encoding.Unicode.GetString( recBuffer, 0, dataSize ) );
            break;
        case NetworkEventType.DisconnectEvent:
            break;
    }
}

}

Server part looks like this

const dgram = require( 'dgram' );
const server = dgram.createSocket( 'udp4' );

server.on( 'listening', () => {
    let address = server.address();
    console.log( 'UDP Server listening on ' + address.address + ":" + address.port );
});

server.on( 'message', function ( message, remote ) {
    console.log( remote.address + ':' + remote.port +' - ' + message.toLocaleString('utf8') );
    server.send(Buffer.from('hello'), remote.port, remote.address, function ( err, bytes ) {
        if ( err ) {
            throw err;
        }
        console.log( `${bytes} bytes sent` );
    });
});

server.bind( 3051, '127.0.0.1' );

Problem is that on server I receive message that I can't decode it. (some strange binary symbols remain). Also, when I'm sending something to client, NetworkTransport.Receive() receives nothing. Thank you.

\$\endgroup\$
  • \$\begingroup\$ You can find the answer to your questions by looking at the Methods itself. For that i recommend using ILSpy. Just drag the UnityEngine.dll file inside it and you'll be able to see all the Functions inside it. \$\endgroup\$ – Aditya Yadav Apr 11 '17 at 7:08
  • \$\begingroup\$ @AdityaYadav, I've made this already, and realization of those method is hidden. It uses some internal thing, so looks like it foreign function from c++. \$\endgroup\$ – metsawyr Apr 11 '17 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.