i made custom message in client project Below is the client code.
public class CustomGameServerMessageTypes
{
public const short ReceiveAuthenticate = 900;
public const short ShutdownMessage = 901;
public const short MaintenanceMessage = 902;
public const short HelloMessage = 903;
}
public struct HelloMessage : NetworkMessage
{
public string data;
public HelloMessage(string value)
{
data = value;
//Debug.Log($"메시지 생성 완료 : {data}, {this.GetType()}");
}
} ////////////////////////////////////////////
public class Startup : MonoBehaviour
{
NewNetworkManager _nm;
MessageWindow _messageWindow;
// Use this for initialization
void Start()
{
_nm = NewNetworkManager.Instance;
_nm.OnDisconnected.AddListener(OnDisconnected);
_nm.OnConnected.AddListener(OnConnected);
NetworkClient.RegisterHandler<ShutdownMessage>(OnServerShutdown);
NetworkClient.RegisterHandler<MaintenanceMessage>(OnMaintenanceMessage);
//NetworkClient.RegisterHandler<HelloMessage>(HelloMessage); -> here i registe my message
_messageWindow = MessageWindow.Instance;
Debug.Log($"네트워크 메니저 인스턴스 아이디: {_nm.GetInstanceID()}");
}
private void OnMaintenanceMessage(MaintenanceMessage msg)
{
var message = msg;
_messageWindow.Title.text = "Maintenance Shutdown scheduled";
_messageWindow.Message.text = string.Format("Maintenance is scheduled for: {0}", message.ScheduledMaintenanceUTC.ToString("MM-DD-YYYY hh:mm:ss"));
_messageWindow.gameObject.SetActive(true);
}
private void OnServerShutdown(ShutdownMessage msg)
{
_messageWindow.Title.text = "Shutdown In Progress";
_messageWindow.Message.text = "Server has issued a shutdown.";
_messageWindow.gameObject.SetActive(true);
NetworkClient.Disconnect();
}
private void OnConnected() -> Send message here!!
{
Debug.Log("connected!@!@!@!@! -> hello message 보내기");
HelloMessage helloMessage = new HelloMessage("안녕하세요");
NetworkClient.Send(helloMessage, Channels.Reliable);
Debug.Log($"메시지 id : {helloMessage.GetType().GetType()}");
}
private void OnDisconnected(int? code)
{
_messageWindow.Title.text = "Disconnected!";
_messageWindow.Message.text = "You were disconnected from the server";
_messageWindow.gameObject.SetActive(true);
}
}
then i got error in my server how i registe my client message in server(my project based in MpsSamples-main\UnityMirror )
registed message in server code already, but it doesn't work Below is the server code.
namespace PlayFab.Networking
{
using System;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.Events;
public class UnityNetworkServer : NetworkManager
{
public static UnityNetworkServer Instance { get; private set; }
public PlayerEvent OnPlayerAdded = new PlayerEvent();
public PlayerEvent OnPlayerRemoved = new PlayerEvent();
public int MaxConnections = 100;
public int Port = 7777; // overwritten by the code in AgentListener.cs
public List<UnityNetworkConnection> Connections
{
get { return _connections; }
private set { _connections = value; }
}
private List<UnityNetworkConnection> _connections = new List<UnityNetworkConnection>();
public class PlayerEvent : UnityEvent<string> { }
// Use this for initialization
public override void Awake()
{
base.Awake();
Instance = this;
NetworkServer.RegisterHandler<ReceiveAuthenticateMessage>(OnReceiveAuthenticate);
NetworkServer.RegisterHandler<HelloMessage>(ReciveHelloMessage);
NetworkClient.RegisterHandler<HelloMessage>(ReciveHelloMessage);
}
private void ReciveHelloMessage(HelloMessage msg)
{
Debug.Log($"메시지 받았다!!!!!! {msg.data}");
}
public void StartListen()
{
this.GetComponent<TelepathyTransport>().port = (ushort)Port;
NetworkServer.Listen(MaxConnections);
}
public override void OnApplicationQuit()
{
base.OnApplicationQuit();
NetworkServer.Shutdown();
}
private void OnReceiveAuthenticate(NetworkConnection nconn, ReceiveAuthenticateMessage message)
{
var conn = _connections.Find(c => c.ConnectionId == nconn.connectionId);
if(conn != null)
{
conn.PlayFabId = message.PlayFabId;
conn.IsAuthenticated = true;
OnPlayerAdded.Invoke(message.PlayFabId);
}
}
private void ReciveHelloMessage(NetworkConnection nconn, HelloMessage msg)
{
Debug.Log($"메시지 받았다!!!!!! {msg.data}");
}
public override void OnServerConnect(NetworkConnection conn)
{
base.OnServerConnect(conn);
Debug.LogWarning("Client Connected");
var uconn = _connections.Find(c => c.ConnectionId == conn.connectionId);
if (uconn == null)
{
_connections.Add(new UnityNetworkConnection()
{
Connection = conn,
ConnectionId = conn.connectionId,
LobbyId = PlayFabMultiplayerAgentAPI.SessionConfig.SessionId
});
}
//HelloMessage();
}
public override void OnServerError(NetworkConnection conn, Exception ex)
{
base.OnServerError(conn, ex);
Debug.Log(string.Format("Unity Network Connection Status: exception - {0}", ex.Message));
}
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
var uconn = _connections.Find(c => c.ConnectionId == conn.connectionId);
if (uconn != null)
{
if (!string.IsNullOrEmpty(uconn.PlayFabId))
{
OnPlayerRemoved.Invoke(uconn.PlayFabId);
}
_connections.Remove(uconn);
}
}
}
[Serializable]
public class UnityNetworkConnection
{
public bool IsAuthenticated;
public string PlayFabId;
public string LobbyId;
public int ConnectionId;
public NetworkConnection Connection;
}
public class CustomGameServerMessageTypes
{
public const short ReceiveAuthenticate = 900;
public const short ShutdownMessage = 901;
public const short MaintenanceMessage = 902;
public const short HelloMessage = 903;
}
public struct ReceiveAuthenticateMessage : NetworkMessage
{
public string PlayFabId;
}
public struct ShutdownMessage : NetworkMessage {}
[Serializable]
public struct MaintenanceMessage : NetworkMessage
{
public DateTime ScheduledMaintenanceUTC;
}
public struct HelloMessage : NetworkMessage
{
public string data;
public HelloMessage(string value)
{
data = value;
Debug.Log($"메시지 생성 완료: {data}, {this.GetType()}");
}
}
}
blow it's my server error
Unknown message id: 14476 for connection: connection(1). This can happen if no handler was registered for this message