首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >团结5 MultiPlayer NetworkTransport.Send

团结5 MultiPlayer NetworkTransport.Send
EN

Stack Overflow用户
提问于 2021-03-31 08:48:24
回答 1查看 133关注 0票数 1

因此,我对Unity很陌生,我需要制作一个多人游戏,在这个游戏中,我可以将客户端与游戏服务器上的接口同步,我已经创建了一个客户机和一个服务器,并且客户机确实连接到了服务器,但问题是它无法发送消息,服务器接收的所有信息都是空的。

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

这是我的Server.cs :

代码语言:javascript
复制
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 :

代码语言:javascript
复制
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");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-02 10:22:52

我已经找到了解决办法,我刚刚补充说

代码语言:javascript
复制
NetworkTransport.Connect(hostId, SERVER_IP, SERVER_WEB_PORT, 0, out error);

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66884432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档