- Home /
Sending Messages with Unet
Hello,
I'm trying to send a Message over the network, like in this example: http://docs.unity3d.com/Manual/UNetMessages.html
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class Begin : NetworkBehaviour
{
const short MyBeginMsg = 1002;
NetworkClient m_client;
public void SendReadyToBeginMessage(int myId)
{
var msg = new IntegerMessage(myId);
m_client.Send(MyBeginMsg, msg);
}
public void Init(NetworkClient client)
{
m_client = client;
NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);
}
void OnServerReadyToBeginMessage(NetworkMessage netMsg)
{
var beginMessage = netMsg.ReadMessage<IntegerMessage>();
Debug.Log("received OnServerReadyToBeginMessage " + beginMessage.value);
}
}
the Init method is never called by the system, Its hard to find a working solution with the docs.
Waht I want to test is:
Sending a MSG to the server and the server sould respond to this message with a other msg.
Answer by xx · Jul 19, 2015 at 07:09 PM
Look at this example, it helped me a lot http://forum.unity3d.com/threads/master-server-sample-project.331979/
Answer by steve-parker-ubm · Feb 19, 2017 at 11:06 PM
Hi sebeisdrache
I know this is an old thread, but for anyone else coming across this You need to Start your server, which is why the client doesn't connect in the code above.
(here is my code - using MatchMaking in 5.5 - but the concept is the same).
public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Match Created");
MatchInfo hostInfo = matchInfo;
NetworkServer.Listen(hostInfo, 9000);
NetworkManager.singleton.StartServer(hostInfo);
**NetworkServer.RegisterHandler(MsgType.Ready, OnClientVoted);**
ActionText.text = "created server";
}
else
{
Debug.LogError("Create match failed");
}
}
Important bit that "got me" was that I was registering the Handlers before starting the server (which is stupid of me). Once I put the registration in AFTER the Server started, the events hook up properly.
Your answer
