Пример #1
0
        void Init()
        {
            node.OnServerStartSuccess += () => OnCreatedChatroom?.Invoke();
            node.OnServerStartFailure += e =>
                                         OnChatroomCreationFailed?.Invoke(e);
            node.OnServerStop += () => OnlosedChatroom?.Invoke();

            node.OnConnectionFailed += ex => OnChatroomJoinFailed?.Invoke(ex);
            node.OnReceiveID        += id => {
                if (id != 0)
                {
                    OnJoinedChatroom?.Invoke(id);

                    OnPeerJoinedChatroom?.Invoke(0); // server joins instantly
                }
            };
            node.OnDisconnected       += () => OnLeftChatroom?.Invoke();
            node.OnRemoteServerClosed += () => OnLeftChatroom?.Invoke();

            node.OnClientJoined += id => OnPeerJoinedChatroom?.Invoke(id);
            node.OnClientLeft   += id => OnPeerLeftChatroom?.Invoke(id);

            node.OnPacketReceived += (sender, packet) => {
                if (packet.Tag.Equals("audio"))
                {
                    var reader    = new BytesReader(packet.Payload);
                    var index     = reader.ReadInt();
                    var frequency = reader.ReadInt();
                    var channels  = reader.ReadInt();
                    var samples   = reader.ReadFloatArray();

                    OnAudioReceived?.Invoke(new ChatroomAudioDTO {
                        id           = sender,
                        segmentIndex = index,
                        frequency    = frequency,
                        channelCount = channels,
                        samples      = samples
                    });
                }
            };
        }
Пример #2
0
        void Start()
        {
            var writer = new BytesWriter();

            writer.WriteInt(90);
            writer.WriteString("Vatsal");
            writer.WriteString("Adrenak");
            writer.WriteString("Ambastha");
            writer.WriteVector3(Vector3.one);
            writer.WriteFloatArray(new[] { 1.1f, 2.2f, 3.3f });

            var reader = new BytesReader(writer.Bytes);

            // READ IN THE SAME ORDER
            Debug.Log(reader.ReadInt());
            Debug.Log(reader.ReadString());
            Debug.Log(reader.ReadString());
            Debug.Log(reader.ReadString());
            Debug.Log(reader.ReadVector3());
            Debug.Log("Floats : " + string.Join(", ", reader.ReadFloatArray()));
        }