Websocket Connected and Subscribbed, but clients are not recieved messages from server

25 Views Asked by At

I am creating a websocket client using the .net Websocket package, and Spring boot is my server, i also have other js clients which they are working correct, no problem about them. The problem is when using the c# client connection is done successfully , also subscription is successfully established, but when i publish a message from java to that topic nothing is recieved. also i need to mention that if i restarted or closed the java server i am getting close session message in the client, but all other messages i do not get. This is my connection and subscription in c#

public async Task ConnectAndSubscribe(String uri,string topic) {
Uri serverUri = new Uri(uri);
await _clientWebSocket.ConnectAsync(serverUri, CancellationToken.None);

// Subscribe to topic with a unique subscription-id
string subscribeMessage = $"SUBSCRIBE\nid:sub-0\ndestination:{topic}\nack:auto\n\n\0";
byte[] subscribeBytes = Encoding.UTF8.GetBytes(subscribeMessage);
await _clientWebSocket.SendAsync(new ArraySegment<byte>(subscribeBytes), 
WebSocketMessageType.Text, true, CancellationToken.None);
 }

And here is how i wait for recieving the message

public async Task StartListening() {
     while (_clientWebSocket.State == WebSocketState.Open)
    {
        var buffer = new byte[1024];
        var result = await _clientWebSocket.ReceiveAsync(new ArraySegment<byte> 
         (buffer), CancellationToken.None);
         if (result.MessageType == WebSocketMessageType.Text)
        {
            string message = Encoding.UTF8.GetString(buffer, 0, result.Count);
            Console.WriteLine($"Received message: {message}");
        }
    } }

and in the java server this is how i publish

socketTemplate.convertAndSend("/topic/test/myTopic,"hello world");

Did anyone faced the same problem before ?

0

There are 0 best solutions below