NetworkTransport message signature and encoding

Hello, 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.

  • Does NetworkTransport.Connect method awaits some message from server?
  • 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;
        }
    }
}

When I’m trying to send something from server to client over UDP, nothing happends on client
Can you help me please?

Okay so… you’re definitely going to make things difficult for yourself here. Building a server-side that mimics what the server-side of unet does will be trivial at best (i’ve seen the formatting of their byte arrays change over time, for the better, so trying to play around with things at that low of a level will be hard to maintain).

Realistically, you have two much better options:

  • Don’t use unet, and implement a more low-level client-side, using something like the c# version of socket.io (GitHub - Quobject/SocketIoClientDotNet: Socket.IO Client Library for .Net). This way, you manage the client-side code and there’s no difficulty in understanding what you’re sending over the network. Much easier to maintain.
  • Don’t use nodejs on the server-side. There’s absolutely no reason to use nodejs on the server-side. There’s been debates in here on this topic as people swarm to the hype of a new modular language. However, in networking, performance is your key concern and c# (which you’re already using on the client-side!) will naturally perform much better than javascript. So, build your server-side and client-side in the same unity project in two separated scenes, then build your “server” scene to a linux headless executable and run that on a linux machine/server… doesn’t get much better than that!

Hopefully this helps. Good luck!

1 Like

Thank you so much for your answer. The only thing why I’m using nodejs - I know it nicely. And as I’m begginner in both Unity and c#, it would take to much effort to learn how to build server with c#.

But I’m okay with any solution, which is good, so maybe I’ll change my mind and will use your suggestion with Unet on both client and server.

Thnak you.

(1) We have plan to publish protocol (not big secret but if you compare this priority with documentation, project examples, bug fixing and performance increasing you will see that priority of this is quite low)
(2) server.dll build is still in beta phase (it means we haven’t decided yet how we should distribute them). This means, that you can ask us about server.dll and try to use them :slight_smile: It is just c++ dll + c# glue code which can talk using unet protocol.

It’s tricky because it’s hard to argue your reasoning, or convince someone in your position otherwise, but having been in your situation many years ago (where I tried to do EVERYTHING with PHP because it’s what I knew) and making many mistakes along the way, i’ll emphasize the importance of using the right tool for the job. So yeah, using what you like and what you know is something you can use when deciding what tech to use for a project, but you should definitely also consider what is most efficient, otherwise you could be limiting the potential of the project. Making mistakes is massively important though, so take this advice with a pinch of salt!

Good luck!

aabramychev, thank you for your answers, and as server.dll might be changed a lot of times until release, I’d better use wise suggestion of donnysobonny and make c# on both sides this time.
Thank you guys.