SharpCaster.Channels.ConnectionChannel.OpenConnection C# (CSharp) Method

OpenConnection() public method

public OpenConnection ( ) : void
return void
        public async void OpenConnection()
        {
            await Write(MessageFactory.Connect());
        }

Usage Example

        public async Task Initialize(string host, string port, ConnectionChannel connectionChannel, HeartbeatChannel heartbeatChannel, Action<Stream, bool> packetReader)
        {
            if (_client == null) _client = new TcpSocketClient();
            await _client.ConnectAsync(host, int.Parse(port), true, default(CancellationToken), true);


            connectionChannel.OpenConnection();
            heartbeatChannel.StartHeartbeat();

            await Task.Run(() =>
            {
                while (true)
                {
                    var sizeBuffer = new byte[4];
                    byte[] messageBuffer = { };
                    // First message should contain the size of message
                    _client.ReadStream.Read(sizeBuffer, 0, sizeBuffer.Length);
                    // The message is little-endian (that is, little end first),
                    // reverse the byte array.
                    Array.Reverse(sizeBuffer);
                    //Retrieve the size of message
                    var messageSize = BitConverter.ToInt32(sizeBuffer, 0);
                    messageBuffer = new byte[messageSize];
                    _client.ReadStream.Read(messageBuffer, 0, messageBuffer.Length);
                    var answer = new MemoryStream(messageBuffer.Length);
                    answer.Write(messageBuffer, 0, messageBuffer.Length);
                    answer.Position = 0;
                    packetReader(answer, true);
                }
            });
        }