Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin Forms Sockets

$
0
0

I don't get it, my friend and I are developing an API and our WebSocket services works, but not the mobile side.. I tried with a couple of clients, on the web, our echo messaging and everything works.

The thing is, I mean, the things seem like the socket is mono-directional. I tried the example of https://github.com/rdavisau/sockets-for-pcl#a-tcp-client:

var address = "127.0.0.1";
var port = 11000;
var r = new Random(); 

var client = new TcpSocketClient();
await client.ConnectAsync(address, port);

// we're connected!
for (int i = 0; i<5; i++)
{
    // write to the 'WriteStream' property of the socket client to send data
    var nextByte = (byte) r.Next(0,254);
    client.WriteStream.WriteByte(nextByte);
    await client.WriteStream.FlushAsync();

    // wait a little before sending the next bit of data
    await Task.Delay(TimeSpan.FromMilliseconds(500)); 
}

await client.DisconnectAsync();

First, after I get connected with this :

public async void ConnectSocketToAPIAsync()
    {
        SocketClient = new TcpSocketClient();
        await SocketClient.ConnectAsync("my.ws.service", 4242);
        ActiveSocketExchange();
    }

    public async void ActiveSocketExchange()
    {
        var bytesRead = -1;
        var buf = new byte[1];

        while (bytesRead != 0)
        {
            bytesRead = await SocketClient.ReadStream.ReadAsync(buf, 0, 1);
            if (bytesRead > 0)
                MessagingCenter.Send((App)Current, SOCKET_Message, System.Text.Encoding.UTF8.GetString(buf, 0, bytesRead));
        }
    }

Everything's fine, my TcpClient is well initialized (even the web-link becomes the http's API addr)

From my page view, when I'm done writing my text, I'm pressing the done button of the keyboard and this code is called:

private void InitSocketPart()
    {
        MessagingCenter.Subscribe<App, string>((App)Application.Current, App.SOCKET_Message, (sender, text) =>
        {
            SocketResponseText = text;
        });
    }

    private async void OnTextCompleted(object sender, EventArgs ea)
    {
        var bytes = Encoding.UTF8.GetBytes(TextToSend);
        try {
            if (App.SocketClient.WriteStream.CanRead)
                Debug.WriteLine("canRead");
            if (App.SocketClient.WriteStream.CanWrite)
                Debug.WriteLine("canWrite");

            App.SocketClient.WriteStream.Write(bytes, 0, TextToSend.Length);
            App.SocketClient.WriteStream.Flush();
        } catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }

So now CanWrite && CanRead are true, but nothing at all happens, even with the use of Async methods... Why so? I don't get it..

The use of messaging center is just to have only one point of incoming message. I'm using it for other things and it works perfectly :)

Thank for any help..


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>