/// <summary>
    /// Hang up call
    /// </summary>
    public void Hang()
    {
        if (CallID != 0)
        {
            DataModel model = new DataModel();
            model.Type    = ChatProtocolType.TYPE_IM;
            model.Request = IMProtocol.IM_HANG;

            IMInfo info = new IMInfo();
            info.UserID = UserID;
            info.CallID = CallID;
            info.UserList.AddRange(ChatPeers);

            model.Message = info.ToByteArray();

            ChatNetworkManager.Instance.Send(model);

            //send udp hang
            UdplDataModel udp = new UdplDataModel();
            udp.Request      = UdpRequest.REQUEST_HANG;
            udp.ChatInfoData = model.Message;
            byte[] udpData = UdpMessageCodec.Encode(udp);

            UdpSocketManager.Instance.Send(udpData);

            //stop udp
            ChatDataHandler.Instance.StopChat();

            CallID = 0;
            ChatPeers.Clear();
            isChatting = false;
        }
    }
    /// <summary>
    /// 挂断
    /// </summary>
    public void Hang()
    {
        if (CallID != 0)
        {
            ProtocolDataModel pd = new ProtocolDataModel();
            pd.Type    = ProtocolType.TYPE_IM;
            pd.Request = IMProtocol.IM_HANG_CRES;

            IMInfo info = new IMInfo();
            info.PeerID = ChatPeerID;
            pd.Message  = info.ToByteArray();

            NetWorkManager.Instance.Send(pd);

            //send udp hang
            CallInfo callInfo = new CallInfo();
            callInfo.CallID = CallID;

            UdplDataModel model = new UdplDataModel();
            model.Request      = RequestByte.REQUEST_HANG;
            model.ChatInfoData = callInfo.ToByteArray();
            byte[] data = UdpMessageCodec.Encode(model);

            UdpSocketManager.Instance.Send(UdpMessageCodec.Encode(model));

            //结束udp传输
            ChatDataHandler.Instance.StopChat();

            CallID = 0;
        }
    }
    /// <summary>
    /// Send audio data
    /// </summary>
    void SendAudio()
    {
        //Get audio data by SDK
        AudioPacket packet = UnityChatSDK.Instance.GetAudio();

        if (packet != null)
        {
            packet.Id = ChatManager.Instance.UserID;
            byte[] audio = GetPbAudioPacket(packet).ToByteArray();

            //UDP Send data to server
            if (audio != null)
            {
                UdplDataModel model = new UdplDataModel();
                model.Request = UdpRequest.REQUEST_AUDIO;

                IMInfo info = new IMInfo();
                info.UserID = ChatManager.Instance.UserID;
                info.CallID = ChatManager.Instance.CallID;
                info.UserList.Add(ChatManager.Instance.ChatPeers);

                model.ChatInfoData = info.ToByteArray();
                model.ChatData     = audio;

                UdpSocketManager.Instance.Send(UdpMessageCodec.Encode(model));
            }
        }
    }
Beispiel #4
0
    public void SendPeerMessage(byte[] message, MessageType t)
    {
        byte[] type = new byte[1] {
            (byte)t
        };
        byte[] data = new byte[message.Length + 1];
        Buffer.BlockCopy(type, 0, data, 0, 1);
        Buffer.BlockCopy(message, 0, data, 1, message.Length);

        IMInfo info = new IMInfo();

        info.UserID   = ChatManager.Instance.UserID;
        info.PeerID   = ChatUIManager.Instance.SelectFriendID;
        info.UserName = ChatManager.Instance.UserName;

        info.MessageData = ByteString.CopyFrom(data);

        ProtocolDataModel pd = new ProtocolDataModel();

        pd.Type    = ProtocolType.TYPE_IM;
        pd.Request = IMProtocol.IM_SENMESSAGE_CRES;
        pd.Message = info.ToByteArray();

        NetWorkManager.Instance.Send(pd);
    }
    /// <summary>
    /// Send audio data
    /// </summary>
    void SendAudio()
    {
        //Get audio data by SDK
        AudioPacket packet = UnityChatSDK.Instance.GetAudio();

        if (packet != null)
        {
            packet.Id = ChatManager.Instance.UserID;
            byte[] audio = GetPbAudioPacket(packet).ToByteArray();

            switch (NetType)
            {
            case NetType.UdpStream:
                //UDP Send data to server
                if (audio != null)
                {
                    UdplDataModel model = new UdplDataModel();
                    model.Request = UdpRequest.REQUEST_AUDIO;

                    IMInfo info = new IMInfo();
                    info.UserID = ChatManager.Instance.UserID;
                    info.CallID = ChatManager.Instance.CallID;
                    info.UserList.Add(ChatManager.Instance.ChatPeers);

                    model.ChatInfoData = info.ToByteArray();
                    model.ChatData     = audio;

                    UdpSocketManager.Instance.Send(UdpMessageCodec.Encode(model));
                }
                break;

            case NetType.TcpStream:

                if (audio != null)
                {
                    DataModel model = new DataModel();
                    model.Type    = ChatProtocolType.TYPE_CHATDATA;
                    model.Request = ChatDataProtocol.CHAT_AUDIO;
                    model.Message = audio;
                    ChatNetworkManager.Instance.Send(model);
                }

                break;

            default:
                break;
            }
        }
    }
    /// <summary>
    /// Send video data
    /// </summary>
    IEnumerator SendVideo()
    {
        //Get video data by SDK
        VideoPacket packet = UnityChatSDK.Instance.GetVideo();

        if (UnityChatSDK.Instance.EnableSync)
        {
            if (packet != null)
            {
                videoPacketQueue.Enqueue(packet);
            }

            if (videoPacketQueue.Count >= UnityChatSDK.Instance.Framerate / UnityChatSDK.Instance.AudioSample)
            {
                packet = videoPacketQueue.Dequeue();
            }
            else
            {
                yield return(null);
            }
        }

        if (packet != null)
        {
            packet.Id = ChatManager.Instance.UserID;
            byte[] video = GetPbVideoPacket(packet).ToByteArray();

            udpPacketIndex++;
            List <UdpPacket> list = UdpPacketSpliter.Split(udpPacketIndex, video, ChunkLength);

            UdplDataModel model = new UdplDataModel();
            model.Request = UdpRequest.REQUEST_VIDEO;
            IMInfo info = new IMInfo();
            info.UserID = ChatManager.Instance.UserID;
            info.CallID = ChatManager.Instance.CallID;
            info.UserList.Add(ChatManager.Instance.ChatPeers);
            model.ChatInfoData = info.ToByteArray();

            for (int i = 0; i < list.Count; i++)
            {
                model.ChatData = UdpPacketEncode(list[i]);
                UdpSocketManager.Instance.Send(UdpMessageCodec.Encode(model));
                yield return(new WaitForSeconds(0.01f));
            }
        }
    }
    /// <summary>
    /// 接听
    /// </summary>
    public void Accept()
    {
        ProtocolDataModel pd = new ProtocolDataModel();

        pd.Type    = ProtocolType.TYPE_IM;
        pd.Request = IMProtocol.IM_ACCEPT_CRES;

        IMInfo info = new IMInfo();

        info.UserName = UserName;
        info.UserID   = UserID;
        info.PeerID   = ChatPeerID;
        //info.CallType = type;
        pd.Message = info.ToByteArray();
        NetWorkManager.Instance.Send(pd);

        //开始udp传输
        ChatDataHandler.Instance.StartChat();
    }
    /// <summary>
    /// Accept call invitation
    /// </summary>
    public void Accept(int userId, List <UserInfo> peerId)
    {
        DataModel model = new DataModel();

        model.Type    = ChatProtocolType.TYPE_IM;
        model.Request = IMProtocol.IM_ACCEPT;

        IMInfo info = new IMInfo();

        info.UserID = userId;
        info.UserList.AddRange(peerId);

        model.Message = info.ToByteArray();

        ChatNetworkManager.Instance.Send(model);

        //Start udp transmission
        ChatDataHandler.Instance.StartChat();
    }
Beispiel #9
0
    //发送udp心跳包
    IEnumerator SendHeart()
    {
        print("start heart...");
        while (isRunning)
        {
            yield return(new WaitForSeconds(0.5f));

            IMInfo info = new IMInfo();
            info.UserID = ChatManager.Instance.UserID;
            info.CallID = ChatManager.Instance.CallID;

            UdplDataModel model = new UdplDataModel();
            model.ChatInfoData = info.ToByteArray();
            model.Request      = UdpRequest.REQUEST_HEART;

            byte[] data = UdpMessageCodec.Encode(model);
            Send(data);
        }
        print("stop heart...");
    }
    public void Call(long callID, ChatType type, int userId, List <UserInfo> peerId)
    {
        if (CallID == 0)
        {
            isChatting = true;
            CallID     = callID;
            DataModel model = new DataModel();
            model.Type    = ChatProtocolType.TYPE_IM;
            model.Request = IMProtocol.IM_CALL;

            IMInfo info = new IMInfo();
            info.CallID = callID;
            info.Type   = (int)type;
            info.UserID = userId;
            info.UserList.AddRange(peerId);

            model.Message = info.ToByteArray();
            ChatNetworkManager.Instance.Send(model);
        }
    }
    /// <summary>
    /// 呼叫
    /// </summary>
    /// <param name="callID">呼叫ID</param>
    /// <param name="type">呼叫类型 1:音频 2:视频</param>
    /// <param name="from">呼叫者 ID</param>
    /// <param name="to">被呼叫者ID </param>
    public void Call(long callID, ChatType type, int from, int to)
    {
        if (CallID == 0)
        {
            CallID = callID;
            ProtocolDataModel pd = new ProtocolDataModel();
            pd.Type    = ProtocolType.TYPE_IM;
            pd.Request = IMProtocol.IM_CALL_CRE;

            IMInfo info = new IMInfo();
            info.UserName = UserName;
            info.UserID   = UserID;
            info.CallID   = callID;
            info.CallType = (int)type;

            ChatDataHandler.Instance.ChatType = type;

            info.PeerID = to;
            ChatPeerID  = to;
            pd.Message  = info.ToByteArray();
            NetWorkManager.Instance.Send(pd);
        }
    }
    /// <summary>
    /// Send video data
    /// </summary>
    IEnumerator SendVideo()
    {
        //Get video data by SDK
        VideoPacket packet = UnityChatSDK.Instance.GetVideo();

        if (UnityChatSDK.Instance.EnableSync)
        {
            if (packet != null)
            {
                videoPacketQueue.Enqueue(packet);
            }

            if (videoPacketQueue.Count >= UnityChatSDK.Instance.Framerate / UnityChatSDK.Instance.AudioSample)
            {
                packet = videoPacketQueue.Dequeue();
            }
            else
            {
                yield return(null);
            }
        }

        if (packet != null)
        {
            packet.Id = ChatManager.Instance.UserID;
            byte[] video = GetPbVideoPacket(packet).ToByteArray();

            switch (NetType)
            {
            case NetType.UdpStream:

                udpPacketIndex++;
                List <UdpPacket> list = UdpPacketSpliter.Split(udpPacketIndex, video, ChunkLength);

                UdplDataModel model = new UdplDataModel();
                model.Request = UdpRequest.REQUEST_VIDEO;
                IMInfo info = new IMInfo();
                info.UserID = ChatManager.Instance.UserID;
                info.CallID = ChatManager.Instance.CallID;
                info.UserList.Add(ChatManager.Instance.ChatPeers);
                model.ChatInfoData = info.ToByteArray();

                for (int i = 0; i < list.Count; i++)
                {
                    model.ChatData = UdpPacketEncode(list[i]);
                    UdpSocketManager.Instance.Send(UdpMessageCodec.Encode(model));
                    yield return(new WaitForSeconds(0.01f));
                }

                break;

            case NetType.TcpStream:

                if (video != null)
                {
                    DataModel tcpVideoData = new DataModel();
                    tcpVideoData.Type    = ChatProtocolType.TYPE_CHATDATA;
                    tcpVideoData.Request = ChatDataProtocol.CHAT_VIDEO;
                    tcpVideoData.Message = video;
                    ChatNetworkManager.Instance.Send(tcpVideoData);
                }

                break;

            default:
                break;
            }
        }
    }