0

I have websocket listner in this way in JavaScript.

var connectionLostMessage = null;

    function websocketConnect() {
        var conn = new ab.Session('ws://abc.xyz.dd:8080?time=1627624930.3964',
            function () {
                clearTimeout(connectionLostMessage);
                connectionLostMessage = null;

                conn.subscribe('316627', function (topic, data) {
                    //some code to use data display
                });
            },
            function () {
                //When connection fails
                //Connection fail handling

                setTimeout(function () {
                    websocketConnect();
                }, 1000)
            },
            {'skipSubprotocolCheck': true}
        );
    }

    websocketConnect();

I want to connect and subscribe to the channel using c# as found in JavaScript in following line.

conn.subscribe('316627', function (topic, data) {

I have tried with in this way but unable to subscribe in the way above.

public async Task WebsocketClientTest()
{
    var url = new Uri("ws://abc.xyz.dd:8080");
    var exitEvent = new ManualResetEvent(false);

    using (var client = new WebsocketClient(url))
    {
        client.MessageReceived
            .Subscribe(msg => Console.WriteLine($"msg.Text: {msg.Text},  msg.MessageType: {msg.MessageType}"));
        
        await client.Start();
        exitEvent.WaitOne();
    }

    Console.ReadLine();
}

I need to write a console application in .Net Core. Please suggest any help or third party compatible with .Net Core 3.1

Many thanks!

  • You need to send a message before receiving. So the message received you be after the WaitOne. – jdweng Jul 29 '21 at 14:33
  • Hi jdweng, I tried the way as suggested but not works. So, I have updated my question to explain more in detail the situation I have. Please see the updated question and suggest how I should proceed. thanks! – Naveed Ahmad Jul 30 '21 at 06:25
  • Where are you writing to the client? – jdweng Jul 30 '21 at 12:36
  • I am writing the client in c# console application. The aim is to capture the data as per channel ID as shown in Javascript example above. – Naveed Ahmad Jul 30 '21 at 16:45
  • You are missing parameters new ab.Session('ws://abc.xyz.dd:8080?time=1627624930.3964' and headers conn.subscribe('316627', function (topic, data) – jdweng Jul 30 '21 at 20:29
  • Yes, but how this can be done in c#? – Naveed Ahmad Jul 31 '21 at 03:27
  • The parameters are part of the URI. Headers like UPGRADE are shown here : https://stackoverflow.com/questions/10200910/creating-a-hello-world-websocket-example – jdweng Jul 31 '21 at 09:56
  • The shared other thread also describing the solution for client in Javascript. In my case, I have working example of client in Javascript. I want to write similar client using c# in console application. – Naveed Ahmad Jul 31 '21 at 19:12
  • Thanks @jdweng your first answer is actually the solution. I was sending the message in client in wrong format. – Naveed Ahmad Aug 12 '21 at 06:18

1 Answers1

0

I found the solution this way as proposed by jdweng in above comments. The only thing I was doing wrong the format of the message to send. My question is also missing the send line after question editing.

client.Send($"[5, \"123456"]");