这似乎是一个操作顺序问题,但我不知道我在哪里把事情搞砸了。我试图在按下按钮时从客户端发送消息到由客户端名称组成的服务器:
string msg = "TELLCLIENTNAME|" + playerName;
SendMsg(msg, reliableChannelID);
在服务器脚本上我有一个案例
case "TELLCLIENTNAME":
OnTellClientName(splitData[1], recConnectionID);
break;
查找字符串,一旦收到该字符串,我只想通过文字反馈向屏幕发送确认信息:
void OnTellClientName(string playerName, int cnnID) {
infoDisplayText.GetComponent<Text>().text += "Player Name : " + playerName + " on Connection ID : " + cnnID + "\n";}
很简单,但我没有让事情有效,我得到的错误是:
Attempt to send to not connected connection {1}
UnityEngine.Networking.NetworkTransport:Send(Int32, Int32, Int32, Byte[], Int32, Byte&)
Client:SendMsg(String, Int32) (at Assets/_Scripts/Networking/Client.cs:96)
Client:Connect() (at Assets/_Scripts/Networking/Client.cs:90)
UnityEngine.EventSystems.EventSystem:Update()
我在这里弄错了什么?
Client.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Client : MonoBehaviour
{
public Loader load;
public GameObject player;
private int ourClientID;
private int connectionID;
private const int maxConnections = 100;
private int port = 5708;
private int hostID;
private int webHostID;
public int reliableChannelID { get; set; }
public int reliableSeqChannelID { get; set; }
public int unreliableSeqChannelID { get; set; }
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
public string playerName { get; set; }
private void Update()
{
if (!isConnected)
return;
Debug.Log("Client is connected to server.");
int recHostID;
int recConnectionID;
int recChannelID;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
switch (recNetworkEvent)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
default:
Debug.Log("Invalid message : " + msg);
break;
}
break;
Debug.Log("Receiving : " + msg);
}
}
public void Connect()
{
Instantiate(player);
playerName = GameManager.playerID;
NetworkTransport.Init();
ConnectionConfig config = new ConnectionConfig();
reliableChannelID = config.AddChannel(QosType.Reliable);
reliableSeqChannelID = config.AddChannel(QosType.ReliableSequenced);
unreliableSeqChannelID = config.AddChannel(QosType.UnreliableSequenced);
HostTopology topo = new HostTopology(config, maxConnections);
//hostID = NetworkTransport.AddHost(topo, port, null);
hostID = NetworkTransport.AddHost(topo, 0);
connectionID = NetworkTransport.Connect(hostID, load.clientIPList[0], port, 0, out error);
connectionTime = Time.time;
isConnected = true;
string msg = "TELLCLIENTNAME|" + playerName;
SendMsg(msg, reliableChannelID);
}
public void SendMsg(string m, int channelID)
{
byte[] msg = Encoding.Unicode.GetBytes(m); // Convert string into byte array
NetworkTransport.Send(hostID, connectionID, channelID, msg, msg.Length * sizeof(char), out error);
Debug.Log("Error : " + error.ToString());
Debug.Log("Sending : " + m);
}
public void Disconnect()
{
NetworkTransport.Disconnect(hostID, connectionID, out error);
}
}
Server.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class ServerClient : MessageBase
{
public int connectionID;
public string playerName;
public string animalType;
public byte[] playerTexBytes;
}
public class Server : MonoBehaviour
{
private List<ServerClient> clients = new List<ServerClient>();
private int connectionID;
private const int maxConnections = 100;
private int port = 5708;
private int hostID;
private int webHostID;
public int reliableChannelID { get; set; }
public int reliableSeqChannelID { get; set; }
public int unreliableSeqChannelID { get; set; }
private bool isStarted = false;
private byte error;
public GameObject playerObject;
public Dictionary<int, GameObject> players = new Dictionary<int, GameObject>();
GameObject infoDisplayText;
GameObject networkTitle;
string networkMsg;
private void Awake()
{
infoDisplayText = GameObject.Find("InfoDisplay");
networkTitle = GameObject.Find("NetworkTitle");
}
private void Start()
{
NetworkTransport.Init();
ConnectionConfig config = new ConnectionConfig();
reliableChannelID = config.AddChannel(QosType.Reliable);
reliableSeqChannelID = config.AddChannel(QosType.ReliableSequenced);
unreliableSeqChannelID = config.AddChannel(QosType.UnreliableSequenced);
HostTopology topo = new HostTopology(config, maxConnections);
hostID = NetworkTransport.AddHost(topo, port, null);
webHostID = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
//DEBUG
networkTitle.GetComponent<Text>().text = "HOST";
infoDisplayText.GetComponent<Text>().text += "Host Started, IP : " + Network.player.ipAddress + "\n";
infoDisplayText.GetComponent<Text>().text += "Socket open, Host ID is " + hostID + "\n";
infoDisplayText.GetComponent<Text>().text += "My player name is " + GameManager.playerID + "\n";
}
private void Update()
{
if (!isStarted)
return;
int recHostID;
int recConnectionID;
int recChannelID;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
switch (recNetworkEvent)
{
case NetworkEventType.ConnectEvent:
OnConnection(recConnectionID);
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "TELLCLIENTNAME":
OnTellClientName(splitData[1], recConnectionID);
break;
case "PLAYERDATA":
OnPlayerData(splitData[1], splitData[2], recConnectionID);
break;
default:
Debug.Log("Invalid message : " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent:
OnDisconnection(recConnectionID);
break;
}
}
void OnConnection(int cnnID)
{
// DEBUG
infoDisplayText.GetComponent<Text>().text += "Player " + cnnID + " has connected.\n";
}
void SendMsg(string message, int channelID, int cnnID)
{
List<ServerClient> c = new List<ServerClient>();
c.Add(clients.Find(x => x.connectionID == cnnID));
SendMsg(message, channelID, c);
}
void SendMsg(string message, int channelID, List<ServerClient> c) // Overload method
{
// DEBUG
infoDisplayText.GetComponent<Text>().text += "Sending : " + message + "\n";
// Convert string into byte array
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach (ServerClient sc in c)
{
NetworkTransport.Send(hostID, sc.connectionID, channelID, msg, msg.Length * sizeof(char), out error);
}
}
void OnTellClientName(string playerName, int cnnID)
{
infoDisplayText.GetComponent<Text>().text += "Player Name : " + playerName + " on Connection ID : " + cnnID + "\n";
}
void OnPlayerData(string playerName, string animalType, int cnnID)
{
// DEBUG
infoDisplayText.GetComponent<Text>().text += "Player Name : " + playerName + "\n";
infoDisplayText.GetComponent<Text>().text += "Animal Type : " + animalType + "\n";
}
void OnDisconnection(int recConnectionID)
{
// DEBUG
infoDisplayText.GetComponent<Text>().text += "Player " + recConnectionID + " has disconnected.\n";
}
}