Example #1
0
        public bool SendText(string channel_id, string text)
        {
            if (!Connected)
            {
                return(false);
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kChannelId] = channel_id;
                mcast_msg[kBounce]    = true;
                mcast_msg[kMessage]   = text;

                SendToChannel(mcast_msg);
            }
            else
            {
                FunChatMessage chat_msg = new FunChatMessage();
                chat_msg.text = text;

                FunMulticastMessage mcast_msg = FunapiMessage.CreateFunMessage(chat_msg, MulticastMessageType.chat);
                mcast_msg.channel = channel_id;
                mcast_msg.bounce  = true;

                SendToChannel(mcast_msg);
            }

            return(true);
        }
        public bool JoinChannel(string channel_id, string token, ChannelMessage handler)
        {
            if (!Connected)
            {
                FunDebug.LogWarning("Multicast.JoinChannel - Multicast is not connected.\n" +
                                    "Please connect first before join a multicast channel.");
                return(false);
            }

            lock (channel_lock_)
            {
                if (channels_.ContainsKey(channel_id))
                {
                    FunDebug.LogWarning("Multicast.JoinChannel - Already joined the '{0} channel.", channel_id);
                    return(false);
                }

                channels_.Add(channel_id, handler);
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kChannelId] = channel_id;
                mcast_msg[kSender]    = sender_;
                mcast_msg[kJoin]      = true;

                if (token != null && token.Length > 0)
                {
                    mcast_msg[kToken] = token;
                }

                session_.SendMessage(kMulticastMsgType, mcast_msg);
            }
            else
            {
                FunMulticastMessage mcast_msg = new FunMulticastMessage();
                mcast_msg.channel = channel_id;
                mcast_msg.sender  = sender_;
                mcast_msg.join    = true;

                if (token != null && token.Length > 0)
                {
                    mcast_msg.token = token;
                }

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg);
            }

            FunDebug.Log("Multicast - Request to join '{0}' channel", channel_id);

            return(true);
        }
Example #3
0
        public bool JoinChannel(string channel_id, ChannelMessage handler)
        {
            if (!Connected)
            {
                FunDebug.Log("Not connected. First connect before join a multicast channel.");
                return(false);
            }

            lock (channel_lock_)
            {
                if (channels_.ContainsKey(channel_id))
                {
                    FunDebug.Log("Already joined the channel: {0}", channel_id);
                    return(false);
                }

                channels_.Add(channel_id, handler);
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kChannelId] = channel_id;
                mcast_msg[kSender]    = sender_;
                mcast_msg[kJoin]      = true;

                session_.SendMessage(kMulticastMsgType, mcast_msg);
            }
            else
            {
                FunMulticastMessage mcast_msg = new FunMulticastMessage();
                mcast_msg.channel = channel_id;
                mcast_msg.sender  = sender_;
                mcast_msg.join    = true;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg);
            }

            FunDebug.Log("Request to join '{0}' channel", channel_id);

            return(true);
        }
        bool sendToChannel(string channel_id, object message)
        {
            if (message == null)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(channel_id))
            {
                FunDebug.LogWarning("[Multicast] can't send a message. invalid channel id.");
                return(false);
            }

            if (!Connected)
            {
                FunDebug.LogWarning("[Multicast] can't send a message. session is not connected.");
                return(false);
            }

            if (!InChannel(channel_id))
            {
                FunDebug.LogWarning("[Multicast] can't send a message. you aren't in '{0}' channel.", channel_id);
                return(false);
            }

            if (encoding_ == FunEncoding.kJson)
            {
                json_helper_.SetStringField(message, kSender, sender_);

                session_.SendMessage(kMulticastMsgType, message, protocol_);
            }
            else
            {
                FunMulticastMessage mcast = message as FunMulticastMessage;
                mcast.sender = sender_;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg, protocol_);
            }

            return(true);
        }
        void requestToJoin(string channel_id, string token)
        {
            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast = new Dictionary <string, object>();
                mcast[kChannelId] = channel_id;
                mcast[kSender]    = sender_;
                mcast[kJoin]      = true;

                if (!string.IsNullOrEmpty(token))
                {
                    mcast[kToken] = token;
                }

                session_.SendMessage(kMulticastMsgType, mcast, protocol_);
            }
            else
            {
                FunMulticastMessage mcast = new FunMulticastMessage();
                mcast.channel = channel_id;
                mcast.sender  = sender_;
                mcast.join    = true;

                if (!string.IsNullOrEmpty(token))
                {
                    mcast.token = token;
                }

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg, protocol_);
            }

            if (!string.IsNullOrEmpty(token))
            {
                lock (token_lock_)
                {
                    tokens_[channel_id] = token;
                }
            }

            FunDebug.Log("[Multicast] requested to join '{0}' channel.", channel_id);
        }
        /// The 'channel_id' field is mandatory.
        /// The 'sender' must fill in the message.
        /// The message shouldn't include join and leave flags.
        public bool SendToChannel(FunMulticastMessage mcast_msg)
        {
            if (mcast_msg == null)
            {
                return(false);
            }

            FunDebug.Assert(!mcast_msg.join);
            FunDebug.Assert(!mcast_msg.leave);

            string channel_id = mcast_msg.channel;

            if (channel_id == "")
            {
                FunDebug.LogWarning("Multicast.SendToChannel - You should set a vaild channel id.");
                return(false);
            }

            lock (channel_lock_)
            {
                if (!Connected)
                {
                    FunDebug.LogWarning("Multicast.SendToChannel - Multicast is not connected.\n" +
                                        "If you are trying to send a message in which you were, " +
                                        "connect first while preserving the session id you used for join.");
                    return(false);
                }
                if (!channels_.ContainsKey(channel_id))
                {
                    FunDebug.LogWarning("Multicast.SendToChannel - You are not in the '{0} channel.", channel_id);
                    return(false);
                }
            }

            mcast_msg.sender = sender_;

            FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);

            session_.SendMessage(kMulticastMsgType, fun_msg);
            return(true);
        }
        void sendLeaveMessage(string channel_id)
        {
            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kChannelId] = channel_id;
                mcast_msg[kSender]    = sender_;
                mcast_msg[kLeave]     = true;

                session_.SendMessage(kMulticastMsgType, mcast_msg);
            }
            else
            {
                FunMulticastMessage mcast_msg = new FunMulticastMessage();
                mcast_msg.channel = channel_id;
                mcast_msg.sender  = sender_;
                mcast_msg.leave   = true;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg);
            }
        }
        public void RequestChannelList()
        {
            if (!Connected)
            {
                FunDebug.LogWarning("[Multicast] request a channel list but the session is not connected.");
                return;
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast = new Dictionary <string, object>();
                mcast[kSender] = sender_;
                session_.SendMessage(kMulticastMsgType, mcast, protocol_);
            }
            else
            {
                FunMulticastMessage mcast = new FunMulticastMessage();
                mcast.sender = sender_;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg, protocol_);
            }
        }
Example #9
0
        public void RequestChannelList()
        {
            if (!Connected)
            {
                FunDebug.Log("Not connected. First connect before join a multicast channel.");
                return;
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kSender] = sender_;
                session_.SendMessage(kMulticastMsgType, mcast_msg);
            }
            else
            {
                FunMulticastMessage mcast_msg = new FunMulticastMessage();
                mcast_msg.sender = sender_;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg);
            }
        }
Example #10
0
        public void RequestChannelList()
        {
            if (!Connected)
            {
                FunDebug.LogWarning("Multicast.RequestChannelList - Multicast is not connected.\n" +
                                    "Please connect first before request a channel list.");
                return;
            }

            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast_msg = new Dictionary <string, object>();
                mcast_msg[kSender] = sender_;
                session_.SendMessage(kMulticastMsgType, mcast_msg);
            }
            else
            {
                FunMulticastMessage mcast_msg = new FunMulticastMessage();
                mcast_msg.sender = sender_;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg);
            }
        }
        void requestToLeave(string channel_id)
        {
            if (encoding_ == FunEncoding.kJson)
            {
                Dictionary <string, object> mcast = new Dictionary <string, object>();
                mcast[kChannelId] = channel_id;
                mcast[kSender]    = sender_;
                mcast[kLeave]     = true;

                session_.SendMessage(kMulticastMsgType, mcast, protocol_);
            }
            else
            {
                FunMulticastMessage mcast = new FunMulticastMessage();
                mcast.channel = channel_id;
                mcast.sender  = sender_;
                mcast.leave   = true;

                FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast);
                session_.SendMessage(kMulticastMsgType, fun_msg, protocol_);
            }

            FunDebug.Log("[Multicast] requested to leave '{0}' channel.", channel_id);
        }