Legacy Dev Forum Posts

 View Only

Sign Up

Correctly logging WebSocketSharp events

  • 1.  Correctly logging WebSocketSharp events

    Posted 06-05-2025 18:06

    Stefan_Eyckmans | 2020-02-14 11:26:33 UTC | #1

    Hi,

    I am trying to log events from NotificationHandler.Websocket as we are getting some unexplicable behavior.

    I use below code to register to the events:

    _notificationHandler = new NotificationHandler();

    notificationHandler.WebSocket.OnOpen += WebSocketOnOpen; notificationHandler.WebSocket.OnClose += WebSocketOnClose; notificationHandler.WebSocket.OnError += WebSocketOnError; _notificationHandler.WebSocket.Log.Level = WebSocketSharp.LogLevel.Trace;

    _notificationHandler.WebSocket.Log.Output = (e, s) => { _logger.LogInformation(new BasicLogContext(e.Message, "WebSocketLog")); };

    the problem is I never get anything for the OnOpen and OnError events, only the OnClose is triggered. From this event I see below in our logging:

    "CloseWebsocket - Websocket closed for user [XXXX] with reason [An exception has occurred while receiving.] and code [1006]. The closure was clean: [False]"

    I have no idea if I am missing useful information since the OnError event is never triggered.

    Am I registering at the moment NotificationHandler is called for the first time, anything wrong with the moment of registration or perhaps an error in my code?

    kr,

    Stefan


    tim.smith | 2020-02-14 23:29:52 UTC | #2

    Stefan_Eyckmans, post:1, topic:7131
    the problem is I never get anything for the OnOpen and OnError events, only the OnClose is triggered.

    The WebSocket is already connected by the time the NotificationHandler constructor returns so the OnOpen event will never be raised after your code has attached its handler.

    Stefan_Eyckmans, post:1, topic:7131
    _notificationHandler.WebSocket.Log.Output

    I'm not sure what that does, but WebSocket.OnMessage is how the SDK gets messages from the websocket. You can see how the messages are handled here:

    https://github.com/MyPureCloud/platform-client-sdk-dotnet/blob/master/build/src/PureCloudPlatform.Client.V2/Extensions/Notifications/NotificationHandler.cs#L116


    system | 2020-03-16 23:29:52 UTC | #3

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 7131