- Home /
UNet. Messaging not work. Please help.
Hi, i want implement automatical room searching in matchmaker. I found or create room. But when i try send message i have error.
Server to clint error:
Empty player list given to NetworkServer.Destroy(), nothing to do. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Client to Server error:
Channels not initialized sending on id '0 UnityEngine.Networking.NetworkConnection:Send(Int16, MessageBase) UNet:OnGUI() (at Assets/UNet.cs:151)
This is my dirty code, more experiments there :)
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.NetworkSystem;
public class UNet : NetworkManager {
NetworkConnection clientConnection;
NetworkConnection serverConnection;
[SerializeField]
NetworkLobbyPlayer lobbyPlayer;
void Start () {
StartMatchMaker();
matchMaker.ListMatches(0, 20, "", OnMatchList);
Debug.Log("Start");
}
void OnMatchList(ListMatchResponse matchList){
Debug.Log("OnMatchListr");
foreach(MatchDesc md in matchList.matches){
Debug.Log(string.Format("roomName = {0}, maxSize = {1}, currentSize = {2}",md.name, md.maxSize, md.currentSize));
if(md.currentSize > 0 && md.currentSize < md.maxSize){
matchMaker.JoinMatch(md.networkId, "", OnMatchJoinedAsClient);
return;
}
}
CreateMatch();
}
public void OnMatchJoinedAsServer(JoinMatchResponse matchJoin){
//base.OnMatchJoined(matchJoin);
Debug.Log("OnMatchJoin as Server");
//StartServer();
StartHost();
}
public void OnMatchJoinedAsClient(JoinMatchResponse matchJoin){
//base.OnMatchJoined(matchJoin);
Debug.Log("OnMatchJoin as Client");
StartClient();
}
void CreateMatch(){
CreateMatchRequest create = new CreateMatchRequest();
create.name = "room1";
create.size = 2;
create.advertise = true;
create.password = "";
NetworkManager.singleton.matchMaker.CreateMatch(create, OnMatchCreate);
}
public void OnMatchCreate(CreateMatchResponse matchResponse)
{
if (matchResponse.success)
matchMaker.JoinMatch(matchResponse.networkId, "", OnMatchJoinedAsServer);
}
public override void OnStartServer ()
{
Debug.Log ("START SERVER");
base.OnStartServer ();
NetworkServer.RegisterHandler(33, OnServer);
//lobbyPlayer.SendReadyToBeginMessage();
}
public override void OnStartHost ()
{
Debug.Log ("START HOST");
base.OnStartServer ();
NetworkServer.RegisterHandler(33, OnServer);
//lobbyPlayer.SendReadyToBeginMessage();
}
public override void OnStartClient (NetworkClient client)
{
Debug.Log ("START CLIENT");
base.OnStartClient (client);
client.RegisterHandler(NetworkMessagesTypes.SERVER_SWITCH, OnServerSwitch);
client.RegisterHandler(33, OnClient);
//lobbyPlayer.SendReadyToBeginMessage();
}
public override void OnServerConnect (NetworkConnection conn)
{
Debug.Log ("OnServerConnect");
//base.OnServerConnect (conn);
clientConnection = conn;
}
public override void OnClientConnect (NetworkConnection conn)
{
Debug.Log ("OnClientConnect");
//base.OnServerConnect (conn);
serverConnection = conn;
//ClientScene.Ready(conn);
//ClientScene.AddPlayer(0);
}
// called when a client is ready
public virtual void OnServerReady(NetworkConnection conn)
{
Debug.Log ("OnServerReady");
NetworkServer.SetClientReady(conn);
}
public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
Debug.Log ("OnServerAddPlayer");
NetworkServer.AddPlayerForConnection(conn, new GameObject(), playerControllerId);
}
void OnServerSwitch(NetworkMessage msg){
Debug.Log ("SWITCH SERVER");
StopClient();
client.handlers.Clear();
StartServer();
}
void OnClient(NetworkMessage msg){
Debug.Log (msg.ReadMessage<NetMSG>().text2);
msg.conn.Send(33, new NetMSG());
}
void OnServer(NetworkMessage msg){
Debug.Log (msg.ReadMessage<NetMSG>().text);
msg.conn.Send(33, new NetMSG());
}
void ServerDisconnect(){
Debug.Log("ServerDisconnect");
clientConnection.Send(NetworkMessagesTypes.SERVER_SWITCH, new NetMSG());
StopServer();
}
public override void OnStopServer ()
{
Debug.Log("OnStopServer");
base.OnStopServer ();
}
void OnGUI(){
if(GUILayout.Button("SwitchServer")){
ServerDisconnect();
}
if(GUILayout.Button("Send To Client")){
clientConnection.Send(33, new NetMSG());
}
if(GUILayout.Button("Send To Server")){
serverConnection.Send(33, new NetMSG());
}
if(GUILayout.Button("Send To ALL")){
NetworkServer.SendToAll(33, new NetMSG());
}
}
void OnApplicationQuit(){
NetworkManager.singleton.matchMaker.DestroyMatch(matchInfo.networkId, OnMatchDestoy);
StopHost();
StopServer();
Network.Disconnect();
}
void OnMatchDestoy(BasicResponse response){
Debug.Log("OnMatchDestoy");
}
}
public class NetworkMessagesTypes : MsgType {
public const short SERVER_SWITCH = 50;
}
Where is ERROR? How create correct automatical matchmaker?
Hi, I'm having similar problem here. Did any of you find solution?
Your answer
Follow this Question
Related Questions
How to send commands from client objects without authority? 0 Answers
.io style matchmaking (one button matchmaking) 1 Answer
How to fix "ClientRpc call on un-spawned object" error, using UNET 2 Answers
Unet Matchmaking Europe-North America | cannot see rooms 1 Answer
Using AssignClientAuthority 0 Answers