- Home /
UNet, any way to identify if the incomming connection is a Browser (ie websocket) or standalone (ie UDP connection)?
As title states I have setup a server client Unet system that works both as standalone and webGL. However when in the editor the client always send its connection as a standalone and then testing in the browser it always sending as websocket. Id like to have the server do different things depending on the type of connection so I can speak to both.
Just wondering what the easiest way to do this is?
thanks.
Answer by BlackHack_ · Aug 15, 2017 at 04:24 PM
The easiest way is to use messages, when the player connects make him send to the server which type of connection he is using, and on the server store that in a dictionary storing the connection and the type.
class MyMsgType
{
public const short SendConnectionDataMsg = MsgType.Highest + 1;
}
class Client : NetworkBehaviour
{
void Start()
{
NetworkManager.singleton.client.RegisterHandler(MsgType.Connect, netMsg =>
{
NetworkManager.singleton.client.Send(MyMsgType.SendConnectionDataMsg, new ConnectionDataNetMsg()
{
isWebPlayer = Application.isWebPlayer
});
});
}
}
class Server : NetworkBehaviour
{
Dictionary<NetworkConnection, bool> connectionTypes = new Dictionary<NetworkConnection, bool>();
void Start()
{
NetworkServer.RegisterHandler(MyMsgType.SendConnectionDataMsg, netMsg =>
{
var msg = netMsg.ReadMessage<ConnectionDataNetMsg>();
connectionTypes[netMsg.conn] = msg.isWebPlayer;
});
}
}
class ConnectionDataNetMsg : MessageBase
{
public bool isWebPlayer;
}
Thanks for the answer but I see from your script that it's using the HLAPI and I'm using the LLAPI so I don't use network behavior or network manager. However I think Since the standalone version is a client I'll simply make the client send a message saying they are a standalone client and therefore default will be browser. That should fix it.
Thanks for your time tho appreciate it but not exactly what I need sadly :( no way to assess if it's correct or not.
Your answer
Follow this Question
Related Questions
UDP Sockets for networking, getting overloaded?? 0 Answers
Local host in WebGL 1 Answer
Does Unity Support TCP or only UDP? 0 Answers
UdpClient EndRecieve recieves a list of null bytes 0 Answers
Receive value from UDP connection 0 Answers