So I am creating UNet multiplayer application but i came to a problem while receiving message.
Application is for android but I also built it for computer.
When I run Server
through unity editor and connect to it with client from computer I have part of code to test connection and send message and it is working and send message when player connects.
When I connect with phone it doesn't send message.
What I know is on phone it return NetworkTransport.Connect()
value as 1
which means that it connected, but I do not think it connected it and i will explain it after code.
Here is my server code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
public class Server : MonoBehaviour
{
private const int MAX_CONNECTION = 100;
private int port = 7777;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted;
private byte error;
private void Start ()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topology, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topology, port, null);
isStarted = true;
}
private void Update()
{
if(!isStarted) { return; }
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch(recData)
{
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
Debug.Log("Player " + connectionId + " has connected");
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Player " + connectionId + " has sent: " + msg);
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("Player " + connectionId + " has disconnected");
break;
}
}
}
And here is Client Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Client : MonoBehaviour
{
private const int MAX_CONNECTION = 100;
private int port = 7777;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private int connectionId;
private float connectionTime;
private bool isConnected;
private bool isStarted = false;
private byte error;
private string playerName;
public Text text;
public void Connect()
{
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if(pName == "")
{
Debug.Log("You must enter a name!");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topology, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if(!isConnected) { return; }
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch(recData)
{
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
break;
case NetworkEventType.DataEvent:
break;
case NetworkEventType.DisconnectEvent:
break;
}
}
}
Here is how i tested application step by step.
For this testing i implemented line which change button text to connectionId
which is connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
and it comes right after that line
- Build application as
.exe
(for pc) - Build and run application as
.apk
(for android) Run application inside editor (android build) - Server scene (which has server script) - Run application on android and pc as clients - Client scene (which has client script)
- Input name and press Connect on PC client - inside unity console pops
Player 1 has connected
- Input name and press Connect on Android client - nothing happens inside unity but button text changes to 1 (which means it connected)
- Again press Connect on PC Client (which add another client) and it should be player 3 since on android connection returned positive but instead it returns
Player 2 has connected
I have no idea what is happening. Is there i need to do for android to connect or maybe because i am connecting with wifi (it was working normally with unity Network Manager but now i can not do it with Lower Api)