【问题标题】:Unity 5 MultiPlayer NetworkTransport.SendUnity 5 MultiPlayer NetworkTransport.Send
【发布时间】:2021-06-27 06:23:31
【问题描述】:

所以我是 Unity 的新手,需要制作一个多人游戏,我可以在其中将客户端与游戏服务器上的界面同步,我创建了一个客户端和一个服务器,客户端确实连接到服务器,但问题是是它无法发送消息,服务器接收到的都是Null

我将服务器作为应用程序运行,客户端需要是 WebGL

这是我的 Server.cs:

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class server : MonoBehaviour
{
    private const int MAX_CONNECTIONS = 2000;
    private const string SERVER_IP = "192.168.1.8";
    private const int SERVER_PORT = 8999;
    private const int SERVER_WEB_PORT = 8998; 
    private const int BUFFER_SIZE = 400000;
    
    private int reliablechannelid;
    private int unreliablechannelid;

    private int hostId;
    private int webHosted;

    private byte[] buffer = new byte[BUFFER_SIZE];
    private bool isInit;
    // Start is called before the first frame update
    void Start()
    {
        GlobalConfig config = new GlobalConfig();
        NetworkTransport.Init(config);

        ConnectionConfig cc = new ConnectionConfig();
        reliablechannelid = cc.AddChannel(QosType.Reliable);
        unreliablechannelid = cc.AddChannel(QosType.Unreliable);
        HostTopology topo = new HostTopology(cc, MAX_CONNECTIONS);

        hostId = NetworkTransport.AddHost(topo, SERVER_PORT);
        webHosted = NetworkTransport.AddWebsocketHost(topo, SERVER_WEB_PORT);
        isInit = true;

    }



    //This function is called when data is sent
    void OnData(int hostId, int connectionId, int channelId, byte[] data, int size, NetworkError error)
    {
        //Here the message being received is deserialized and output to the console
        Stream serializedMessage = new MemoryStream(data);
        BinaryFormatter formatter = new BinaryFormatter();
        string message = formatter.Deserialize(serializedMessage).ToString();

        //Output the deserialized message as well as the connection information to the console
        Debug.Log("OnData(hostId = " + hostId + ", connectionId = "
            + connectionId + ", channelId = " + channelId + ", data = "
            + message + ", size = " + size + ", error = " + error.ToString() + ")");

        Debug.Log("data = " + message);
    }


    // Update is called once per frame
    void Update()
    {
        {
            if (!isInit)
            {
                return;
            }



            int outHostId;
            int outConnectionId;
            int outChannelId;
            byte[] buffer = new byte[1024];
            int receivedSize;
            byte error;

            //Set up the Network Transport to receive the incoming message, and decide what type of event
            NetworkEventType eventType = NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, buffer.Length, out receivedSize, out error);

            switch (eventType)
            {
                //Use this case when there is a connection detected
                case NetworkEventType.ConnectEvent:
                    {
                        //Call the function to deal with the received information
                        Debug.Log("Connected");
                        break;
                    }

                //This case is called if the event type is a data event, like the serialized message
                case NetworkEventType.DataEvent:
                    {
                        //Call the function to deal with the received data
                        OnData(outHostId, outConnectionId, outChannelId, buffer, receivedSize, (NetworkError)error);
                        break;
                    }

                case NetworkEventType.Nothing:
                    break;

                default:
                    //Output the error
                    Debug.LogError("Unknown network message type received: " + eventType);
                    break;
            }
        }

       
       

    }
}

这是我的 Client.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine.UI;
using System.IO;
public class client : MonoBehaviour
{
    private const int MAX_CONNECTIONS = 2000;
    private const string SERVER_IP = "192.168.1.7";
    private const int SERVER_PORT = 8999;
    private const int SERVER_WEB_PORT = 8998;
    private const int BUFFER_SIZE = 400000;
    private int connectionId;
  
    private int reliablechannelid;
    private int unreliableChannelId;

    private int hostId;
    bool ok;

    private byte error;
    // private byte[] buffer = new byte[BUFFER_SIZE];
    private bool isConnected;
    public string Msg = "test";
    public byte[] buffer;
    // Start is called before the first frame update
    void Connect()
    {
        GlobalConfig config = new GlobalConfig();
        NetworkTransport.Init(config);

        ConnectionConfig cc = new ConnectionConfig();
        reliablechannelid = cc.AddChannel(QosType.Reliable);
       
        HostTopology topo = new HostTopology(cc, MAX_CONNECTIONS);

        hostId = NetworkTransport.AddHost(topo, 0);



#if UNITY_WEBGL

        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, SERVER_WEB_PORT, 0, out error);
        Debug.Log((NetworkError)error);
        Debug.Log("connectionId=  "+connectionId);



#else
        connectionId = NetworkTransport.Connect(hostId, SERVER_IP, SERVER_PORT, 0, out error);
        Debug.Log((NetworkError)error);
        Debug.Log("connectionId=  " + connectionId);
#endif


    }

    //This is the function that serializes the message before sending it
    void SendMyMessage(string textInput)
    {
        byte error;
        byte[] buffer = new byte[1024];
        Stream message = new MemoryStream(buffer);
        BinaryFormatter formatter = new BinaryFormatter();
        //Serialize the message
        formatter.Serialize(message, textInput);

        //Send the message from the "client" with the serialized message and the connection information
        NetworkTransport.Send(hostId, connectionId, reliablechannelid, buffer, (int)message.Position, out error);

        //If there is an error, output message error to the console
        if ((NetworkError)error != NetworkError.Ok)
        {
            Debug.Log("Message send error: " + (NetworkError)error);
        }
    }

    // Update is called once per frame
    void Update()
    {
        SendMyMessage("heyyyyy");
    }
}

【问题讨论】:

    标签: c# unity3d networking multiplayer unity-networking


    【解决方案1】:

    我找到了解决办法, 我刚刚加了

    NetworkTransport.Connect(hostId, SERVER_IP, SERVER_WEB_PORT, 0, out error);
    

    也连接到我的服务器,我不知道服务器也需要连接 希望这会对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2022-12-04
      • 1970-01-01
      • 2021-04-15
      相关资源
      最近更新 更多