public void Connect(string hostname_or_ip, ushort port, FunEncoding encoding, bool session_reliability) { // TODO(dkmoon): currenlty only Protobuf is supported. DebugUtils.Assert(encoding == FunEncoding.kProtobuf); // Discards previous instance, if any, and creates a brand new instance. multicasting_ = new FunapiMulticastClient (encoding); multicasting_.Connect(hostname_or_ip, port, session_reliability); }
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 void OnSessionClosed() { DebugUtils.Log("Session closed."); network_ = null; multicast_ = null; chat_ = null; }
public FunapiChatClient(FunapiNetwork network, FunEncoding encoding) { multicasting_ = new FunapiMulticastClient(network, encoding); encoding_ = encoding; }
public void OnGUI() { //---------------------------------------------------------------------------- // FunapiNetwork test //---------------------------------------------------------------------------- with_protobuf_ = GUI.Toggle(new Rect(30, 0, 300, 20), with_protobuf_, " google protocol buffer"); with_session_reliability_ = GUI.Toggle(new Rect(30, 20, 300, 20), with_session_reliability_, " session reliability"); GUI.Label(new Rect(30, 40, 300, 20), "server : " + 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, 110, 240, 40), "Connect (UDP)")) { Connect(TransportProtocol.kUdp); } if (GUI.Button(new Rect(30, 160, 240, 40), "Connect (HTTP)")) { Connect(TransportProtocol.kHttp); } GUI.enabled = (network_ != null && network_.Connected); if (GUI.Button(new Rect(30, 210, 240, 40), "Disconnect")) { Disconnect(); } if (GUI.Button(new Rect(30, 260, 240, 40), "Send 'Hello World'")) { SendEchoMessage(); } //---------------------------------------------------------------------------- // Announcements test //---------------------------------------------------------------------------- GUI.enabled = true; if (GUI.Button(new Rect(30, 340, 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(); } //---------------------------------------------------------------------------- // Resource download test //---------------------------------------------------------------------------- GUI.enabled = downloader_ == null; GUI.Label(new Rect(30, 390, 300, 20), String.Format("server : {0}:{1}", kDownloadServerIp, kDownloadServerPort)); if (GUI.Button(new Rect(30, 410, 240, 40), "File Download (HTTP)")) { if (FunapiConfig.IsValid) { downloader_ = FunapiConfig.CreateDownloader(FunapiUtils.GetLocalDataPath); if (downloader_ != null) { downloader_.UpdateCallback += new FunapiHttpDownloader.UpdateEventHandler(OnDownloadUpdate); downloader_.FinishedCallback += new FunapiHttpDownloader.FinishEventHandler(OnDownloadFinished); } } if (downloader_ == null) { downloader_ = new FunapiHttpDownloader(FunapiUtils.GetLocalDataPath, false, OnDownloadUpdate, OnDownloadFinished); downloader_.StartDownload(string.Format("http://{0}:{1}", kDownloadServerIp, kDownloadServerPort)); } } //---------------------------------------------------------------------------- // FunapiMulticasting test //---------------------------------------------------------------------------- GUI.enabled = (multicast_ == null || !multicast_.Connected); GUI.Label(new Rect(280, 40, 300, 20), "server : " + kMulticastServerIp); string multicast_title = "Multicast (Protobuf) connect"; if (GUI.Button(new Rect(280, 60, 240, 40), multicast_title)) { if (FunapiConfig.IsValid) { multicast_ = FunapiConfig.CreateMulticasting(FunEncoding.kProtobuf, with_session_reliability_); } if (multicast_ == null) { if (multicast_ == null) multicast_ = new FunapiMulticastClient(FunEncoding.kProtobuf); multicast_.Connect(kMulticastServerIp, kMulticastPbufPort, with_session_reliability_); } Debug.Log("Connecting to the multicast server.."); } GUI.enabled = (multicast_ != null && multicast_.Connected && !multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) join"; if (GUI.Button(new Rect(280, 110, 240, 40), multicast_title)) { multicast_.JoinChannel(kMulticastTestChannel, OnMulticastChannelSignalled); Debug.Log(String.Format("Joining the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) send"; if (GUI.Button(new Rect(280, 160, 240, 40), multicast_title)) { 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); Debug.Log(String.Format("Sending a message to the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) leave"; if (GUI.Button(new Rect(280, 210, 240, 40), multicast_title)) { multicast_.LeaveChannel(kMulticastTestChannel); Debug.Log(String.Format("Leaving the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (chat_ == null || !chat_.Connected); string chat_title = "Chat (Protobuf) connect"; if (GUI.Button(new Rect(280, 260, 240, 40), chat_title)) { if (chat_ == null) chat_ = new FunapiChatClient(); chat_.Connect(kMulticastServerIp, kMulticastPbufPort, FunEncoding.kProtobuf, with_session_reliability_); Debug.Log("Connecting to the chat server.."); } GUI.enabled = (chat_ != null && chat_.Connected && !chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) join"; if (GUI.Button(new Rect(280, 310, 240, 40), chat_title)) { chat_.JoinChannel(kChatTestChannel, kChatUserName, OnChatChannelReceived); Debug.Log(String.Format("Joining the chat channel '{0}'", kChatTestChannel)); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) send"; if (GUI.Button(new Rect(280, 360, 240, 40), chat_title)) { chat_.SendText(kChatTestChannel, "hello world"); Debug.Log(String.Format("Sending a message to the chat channel '{0}'", kChatTestChannel)); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) leave"; if (GUI.Button(new Rect(280, 410, 240, 40), chat_title)) { chat_.LeaveChannel(kChatTestChannel); Debug.Log(String.Format("Leaving the chat channel '{0}'", kChatTestChannel)); } }
public static FunapiMulticastClient CreateMulticasting(FunEncoding encoding, bool session_reliability) { if (data_ == null) { Debug.Log("There's no config data. You should call FunapiConfig.Load first."); return null; } string str_ip = "multicast_server_ip"; string str_port = "multicast_server_port"; if (!data_.ContainsKey(str_ip) || !data_.ContainsKey(str_port)) { Debug.Log("CreateMulticasting - Can't find values for multicasting."); return null; } string hostname_or_ip = data_[str_ip] as string; UInt16 port = Convert.ToUInt16(data_[str_port]); if (hostname_or_ip.Length <= 0 || port == 0) { Debug.Log(String.Format("CreateMulticasting - Invalid value. ip:{0} port:{1} encoding:{2}", hostname_or_ip, port, encoding)); return null; } FunapiMulticastClient multicast = new FunapiMulticastClient(encoding); multicast.Connect(hostname_or_ip, port, session_reliability); return multicast; }