im trying to connect to websocket from D365 , I have tried to connect to it using .NET library and using that DLL into my project but , the websocket close before ReciveAsync() and i don’t get any response , u can’t even find out wats the status , not able to get the response back by x++ , I’m able to get maximum no connection exceed error at times in response by RecievAsync() but at times WB closes and there’s no response i have been banging my head round this issue for a while buy can’t find a solution , can anyone provide me sample code for doing this in x++ , doing it via .NET is quite complex
I have edited this thread and added my code here and from here on ill be continuing this thread for futher clarifications
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Threading;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace ASP_NetWBsocket
{
public class Socket
{
private static string receivedMessage;
private static string sharedData = null; // Shared variable to store received data
public static async Task ConnectAsync(System.String uri, string _authToken)
{
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
return true;
};
var httpClient = new HttpClient(handler);
using (ClientWebSocket webSocket = new ClientWebSocket())
{
try
{
// Connect to the WebSocket server
Console.WriteLine("Connecting to WebSocket server...");
webSocket.Options.SetRequestHeader("Authorization", "Bearer" + _authToken);
webSocket.Options.KeepAliveInterval = TimeSpan.FromMinutes(40);
await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
Console.WriteLine("Connected!");
await ReceiveMessagesAsync(webSocket);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
private static async Task ReceiveMessagesAsync(ClientWebSocket webSocket)
{
var message = new StringBuilder();
byte[] buffer = new byte[4096 * 4];
while (webSocket.State == WebSocketState.Open)
{
WebSocketReceiveResult result;
do
{
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); /* comments - The process doesnt halt here its just that my dubugger comes out and im not able to see the message is next line wat is the reason , possibly connections is established or not I dont know cus i dont wat happened also the 2 time when i run the same process result variable populates and gives me the error for maximum connections and i if i dont abort the process if the message type is text when the the code iterates again through while it throws exception " System.Net.WebSockets.WebSocketException (0x83760002): Invalid data format for the specific protocol" */
var messageChunk = Encoding.UTF8.GetString(buffer, 0, result.Count);
message.Append(messageChunk);
} while (!result.EndOfMessage);
Console.WriteLine("Received: " + message.ToString());
if (result.MessageType == WebSocketMessageType.Close)
{
await
webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close response received",
CancellationToken.None);
}
else if (result.MessageType == WebSocketMessageType.Text && result.Count == 147)
{
webSocket.Abort();
}
receivedMessage = message.ToString();
sharedData = receivedMessage; // Store the received data in the shared variable
}
}
public string GetSharedData()
{
return sharedData; // Method to retrieve the shared data
}
}
}