Beispiel #1
0
        void onMulticastChannelReceived(string channel_id, string sender, object body)
        {
            if (multicast_.encoding == FunEncoding.kJson)
            {
                string channel = FunapiMessage.JsonHelper.GetStringField(body, "_channel");
                FunDebug.Assert(channel != null && channel == channel_id);

                string message = FunapiMessage.JsonHelper.GetStringField(body, "_message");
                FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}",
                             channel_id, message);
            }
            else
            {
                FunDebug.Assert(body is FunMulticastMessage);
                FunMulticastMessage mcast_msg = body as FunMulticastMessage;
                FunDebug.Assert(channel_id == mcast_msg.channel);

                object           obj       = FunapiMessage.GetMessage(mcast_msg, MulticastMessageType.pbuf_hello);
                PbufHelloMessage hello_msg = obj as PbufHelloMessage;
                if (hello_msg == null)
                {
                    return;
                }

                FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}",
                             channel_id, hello_msg.message);
            }
        }
Beispiel #2
0
        public void sendMulticastMessage()
        {
            if (multicast_.encoding == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg["_channel"] = kChannelName;
                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 = FunapiMessage.CreateFunMessage(hello_msg, MulticastMessageType.pbuf_hello);
                mcast_msg.channel = kChannelName;
                mcast_msg.bounce  = true;

                multicast_.SendToChannel(mcast_msg);
            }
        }
    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_.sender = "player" + UnityEngine.Random.Range(1, 100);
                multicast_encoding_ = transport.Encoding;

                multicast_.JoinedCallback += delegate(string channel_id, string sender) {
                    DebugUtils.DebugLog("JoinedCallback called. player:{0}", sender);
                };
                multicast_.LeftCallback += delegate(string channel_id, string sender) {
                    DebugUtils.DebugLog("LeftCallback called. player:{0}", sender);
                };
                multicast_.ErrorCallback += new FunapiMulticastClient.ErrorNotify(OnMulticastError);
            }
        }

        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);
        }

        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);
            }
        }

        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);
        }

        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);
                chat_.sender = "player" + UnityEngine.Random.Range(1, 100);

                chat_.JoinedCallback += delegate(string channel_id, string sender) {
                    DebugUtils.DebugLog("JoinedCallback called. player:{0}", sender);
                };
                chat_.LeftCallback += delegate(string channel_id, string sender) {
                    DebugUtils.DebugLog("LeftCallback called. player:{0}", sender);
                };
            }
        }

        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, OnChatChannelReceived);
        }

        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 everyone.");
        }

        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);
        }
    }
    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));
        }
    }