public void OnGUI() { //---------------------------------------------------------------------------- // FunapiNetwork test //---------------------------------------------------------------------------- with_session_reliability_ = GUI.Toggle(new Rect(30, 5, 130, 20), with_session_reliability_, " session reliability"); with_protobuf_ = GUI.Toggle(new Rect(180, 5, 150, 20), with_protobuf_, " google protocol buffer"); GUI.Label(new Rect(30, 40, 300, 20), "[FunapiNetwork] - " + kServerIp); GUI.enabled = (network_ == null || !network_.Started); if (GUI.Button(new Rect(30, 60, 240, 40), "Connect (TCP)")) { Connect(TransportProtocol.kTcp); } if (GUI.Button(new Rect(30, 105, 240, 40), "Connect (UDP)")) { Connect(TransportProtocol.kUdp); } if (GUI.Button(new Rect(30, 150, 240, 40), "Connect (HTTP)")) { Connect(TransportProtocol.kHttp); } GUI.enabled = (network_ != null && network_.Connected); if (GUI.Button(new Rect(30, 195, 240, 40), "Disconnect")) { Disconnect(); } if (GUI.Button(new Rect(30, 240, 240, 40), "Send a message")) { SendEchoMessage(); } //---------------------------------------------------------------------------- // Announcements test //---------------------------------------------------------------------------- GUI.enabled = true; GUI.Label(new Rect(30, 300, 300, 20), string.Format("[Announcer] - {0}:{1}", kAnnouncementIp, kAnnouncementPort)); if (GUI.Button(new Rect(30, 320, 240, 40), "Update announcements")) { if (announcement_ == null) { announcement_ = new FunapiAnnouncement(); announcement_.ResultCallback += new FunapiAnnouncement.EventHandler(OnAnnouncementResult); string url = ""; if (FunapiConfig.IsValid) { url = FunapiConfig.AnnouncementUrl; } if (url.Length <= 0) { url = string.Format("http://{0}:{1}", kAnnouncementIp, kAnnouncementPort); } if (url.Length <= 0) { return; } announcement_.Init(url); } announcement_.UpdateList(5); } //---------------------------------------------------------------------------- // Resource download test //---------------------------------------------------------------------------- GUI.enabled = downloader_ == null; GUI.Label(new Rect(30, 380, 300, 20), string.Format("[Downloader] - {0}:{1}", kDownloadServerIp, kDownloadServerPort)); if (GUI.Button(new Rect(30, 400, 240, 40), "Resource downloader (HTTP)")) { string download_url = ""; if (FunapiConfig.IsValid) { FunapiConfig.GetDownloaderUrl(out download_url); } if (download_url == "") { download_url = string.Format("http://{0}:{1}", kDownloadServerIp, kDownloadServerPort); } downloader_ = new FunapiHttpDownloader(); downloader_.VerifyCallback += new FunapiHttpDownloader.VerifyEventHandler(OnDownloadVerify); downloader_.ReadyCallback += new FunapiHttpDownloader.ReadyEventHandler(OnDownloadReady); downloader_.UpdateCallback += new FunapiHttpDownloader.UpdateEventHandler(OnDownloadUpdate); downloader_.FinishedCallback += new FunapiHttpDownloader.FinishEventHandler(OnDownloadFinished); downloader_.GetDownloadList(download_url, FunapiUtils.GetLocalDataPath); } //---------------------------------------------------------------------------- // FunapiMulticasting test //---------------------------------------------------------------------------- GUI.enabled = (multicast_ == null); GUI.Label(new Rect(280, 40, 300, 20), "[Muticasting]"); string multicast_title = "Create 'multicast'"; if (GUI.Button(new Rect(280, 60, 240, 40), multicast_title)) { FunapiTransport transport = null; if (network_ == null || (transport = network_.GetTransport(TransportProtocol.kTcp)) == null) { DebugUtils.LogWarning("You should connect to tcp transport first."); } else { multicast_ = new FunapiMulticastClient(network_, transport.Encoding); multicast_encoding_ = transport.Encoding; } } GUI.enabled = (multicast_ != null && multicast_.Connected && !multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Join a channel"; if (GUI.Button(new Rect(280, 105, 240, 40), multicast_title)) { multicast_.JoinChannel(kMulticastTestChannel, OnMulticastChannelSignalled); DebugUtils.Log("Joining the multicast channel '{0}'", kMulticastTestChannel); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Send a message"; if (GUI.Button(new Rect(280, 150, 240, 40), multicast_title)) { if (multicast_encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg["_channel"] = kMulticastTestChannel; mcast_msg["_bounce"] = true; mcast_msg["message"] = "multicast test message"; multicast_.SendToChannel(mcast_msg); } else { /*PbufHelloMessage hello_msg = new PbufHelloMessage(); * hello_msg.message = "multicast test message"; * * FunMulticastMessage mcast_msg = new FunMulticastMessage(); * mcast_msg.channel = kMulticastTestChannel; * mcast_msg.bounce = true; * Extensible.AppendValue(mcast_msg, (int)MulticastMessageType.pbuf_hello, hello_msg); * * multicast_.SendToChannel(mcast_msg);*/ } DebugUtils.Log("Sending a message to the multicast channel '{0}'", kMulticastTestChannel); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Leave a channel"; if (GUI.Button(new Rect(280, 195, 240, 40), multicast_title)) { multicast_.LeaveChannel(kMulticastTestChannel); DebugUtils.Log("Leaving the multicast channel '{0}'", kMulticastTestChannel); } GUI.Label(new Rect(280, 250, 300, 20), "[Multicast Chat]"); GUI.enabled = (chat_ == null); string chat_title = "Create 'chat'"; if (GUI.Button(new Rect(280, 270, 240, 40), chat_title)) { FunapiTransport transport = null; if (network_ == null || (transport = network_.GetTransport(TransportProtocol.kTcp)) == null) { DebugUtils.LogWarning("You should connect to tcp transport first."); } else { chat_ = new FunapiChatClient(network_, transport.Encoding); } } GUI.enabled = (chat_ != null && chat_.Connected && !chat_.InChannel(kChatTestChannel)); chat_title = "Join a channel"; if (GUI.Button(new Rect(280, 315, 240, 40), chat_title)) { chat_.JoinChannel(kChatTestChannel, kChatUserName, OnChatChannelReceived); DebugUtils.Log("Joining the chat channel '{0}'", kChatTestChannel); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Send a message"; if (GUI.Button(new Rect(280, 360, 240, 40), chat_title)) { chat_.SendText(kChatTestChannel, "hello world"); DebugUtils.Log("Sending a message to the chat channel '{0}'", kChatTestChannel); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Leave a channel"; if (GUI.Button(new Rect(280, 405, 240, 40), chat_title)) { chat_.LeaveChannel(kChatTestChannel); DebugUtils.Log("Leaving the chat channel '{0}'", kChatTestChannel); } }
private FunapiTransport GetNewTransport(TransportProtocol protocol) { FunapiTransport transport = null; FunEncoding encoding = with_protobuf_ ? FunEncoding.kProtobuf : FunEncoding.kJson; if (FunapiConfig.IsValid) { transport = FunapiConfig.CreateTransport(protocol, encoding); } if (transport == null) { if (protocol == TransportProtocol.kTcp) { transport = new FunapiTcpTransport(kServerIp, (ushort)(with_protobuf_ ? 8022 : 8012), encoding); transport.AutoReconnect = true; //transport.EnablePing = true; //transport.DisableNagle = true; //((FunapiTcpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption); } else if (protocol == TransportProtocol.kUdp) { transport = new FunapiUdpTransport(kServerIp, (ushort)(with_protobuf_ ? 8023 : 8013), encoding); // Please set the same encryption type as the encryption type of server. //((FunapiUdpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption); } else if (protocol == TransportProtocol.kHttp) { transport = new FunapiHttpTransport(kServerIp, (ushort)(with_protobuf_ ? 8028 : 8018), false, encoding); // Send messages using WWW class //((FunapiHttpTransport)transport).UseWWW = true; // Please set the same encryption type as the encryption type of server. //((FunapiHttpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption); } } if (transport != null) { transport.StartedCallback += new TransportEventHandler(OnTransportStarted); transport.StoppedCallback += new TransportEventHandler(OnTransportClosed); transport.FailureCallback += new TransportEventHandler(OnTransportFailure); // Connect timeout. transport.ConnectTimeoutCallback += new TransportEventHandler(OnConnectTimeout); transport.ConnectTimeout = 10f; // If you prefer use specific Json implementation other than Dictionary, // you need to register json accessors to handle the Json implementation before FunapiNetwork::Start(). // E.g., transport.JsonHelper = new YourJsonAccessorClass // Adds extra server list // Use HostHttp for http transport. //transport.AddServerList(new List<HostAddr>{ // new HostAddr("127.0.0.1", 8012), new HostAddr("127.0.0.1", 8012), // new HostAddr("127.0.0.1", 8013), new HostAddr("127.0.0.1", 8018) //}); } return(transport); }