82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using FishNet;
|
|
using FishNet.Transporting;
|
|
using TMPro;
|
|
using Types.Broadcasts;
|
|
using UnityEngine;
|
|
|
|
namespace UI
|
|
{
|
|
public class NetworkChatUIController : MonoBehaviour
|
|
{
|
|
private void Start()
|
|
{
|
|
Debug.Log("NetworkChatUIController Start");
|
|
InstanceFinder.ServerManager.OnServerConnectionState += OnServerConnectionState;
|
|
InstanceFinder.ClientManager.OnClientConnectionState += OnClientConnectionState;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
// InstanceFinder.ClientManager.RegisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
InstanceFinder.ClientManager.UnregisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
|
|
public void OnKeyDown_Enter()
|
|
{
|
|
Debug.Log("OnKeyDown_Enter");
|
|
var chatBroadcast = new ChatBroadcast
|
|
{
|
|
Message = "Hello World",
|
|
NetworkTime = Time.time,
|
|
Username = "Player",
|
|
FontColor = Color.white
|
|
};
|
|
|
|
InstanceFinder.ClientManager.Broadcast(chatBroadcast);
|
|
}
|
|
|
|
// on click send
|
|
public void OnClick_Send()
|
|
{
|
|
var messageInput = GetComponent<TMP_InputField>();
|
|
var chatBroadcast = new ChatBroadcast
|
|
{
|
|
Message = messageInput.text,
|
|
NetworkTime = Time.time,
|
|
Username = "Player",
|
|
FontColor = Color.white
|
|
};
|
|
|
|
InstanceFinder.ClientManager.Broadcast(chatBroadcast);
|
|
messageInput.text = "";
|
|
}
|
|
|
|
private void OnServerConnectionState(ServerConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("Server Connection State Changed " + obj.ConnectionState);
|
|
}
|
|
|
|
private void OnClientConnectionState(ClientConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("netchat-ui: Client Connection State Changed " + obj.ConnectionState);
|
|
var clientState = obj.ConnectionState;
|
|
if (clientState == LocalConnectionState.Started)
|
|
InstanceFinder.ClientManager.RegisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
else if (clientState == LocalConnectionState.Stopped)
|
|
InstanceFinder.ClientManager.UnregisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
}
|
|
|
|
private void OnChatBroadcast(ChatBroadcast broadcast, Channel channel)
|
|
{
|
|
Debug.Log("ChatBroadcast received");
|
|
// Debug.Log(broadcast.Message, broadcast.FontColor, broadcast.Username);
|
|
|
|
Debug.Log("ChatBroadcast received from: " + broadcast.Username + " at " + broadcast.NetworkTime);
|
|
Debug.Log("ChatBroadcast message: " + broadcast.Message);
|
|
}
|
|
}
|
|
} |