none
WebSocketClient Connection Header values RRS feed

  • Question

  • I have a service which I connect to with no problems using javascript in browsers and using a websocket client library in python.

    In all cases I am just using default option...I connect, send text, receive test in reply.

    I am trying to connect to this server using ClientWebSocket(), but on calling ConnectAsync I get the exception:

         WebSocketException: The 'Connection' header value 'Upgrade,Keep-Alive' is invalid.

    I have tried setting a value to timeout in the options, and that does not change anything.

    The trouble is that I'm not even sure about the origin of this exception, let alone how to fix it.

    Looking into the details I can see that:

      "Exception has been thrown by the target of an invocation."

    Does that mean underlying code, or is that a return message from the server?

    Any help or suggestions appreciated!

    Thursday, November 7, 2019 3:33 PM

All replies

  • Hi Bellisarius,

    Thank you for posting here.

    According to your description, I have a question to confirm with you.

    Could you provide some related code here? It will help us to analyze and reproduce your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 8, 2019 8:06 AM
    Moderator
  • Here's the code:

            private void tst()
            {
                GetConnection(new Uri("ws://10.10.1.27:2500"), 2000);
            }
    
            private ClientWebSocket sck;
            private async void GetConnection(Uri server ,int timeout)
            {
                sck = new ClientWebSocket();
                sck.Options.KeepAliveInterval = TimeSpan.FromMinutes(10);
    
                using (var cts = new CancellationTokenSource(timeout))
                {
                    await sck.ConnectAsync(server, cts.Token);
                        
                }
    
    
            }
    

    And here are the details of the Exception:

    System.Reflection.TargetInvocationException
      HResult=0x80131604
      Message=Exception has been thrown by the target of an invocation.
      Source=mscorlib
      StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)


    Inner Exception 1:
    WebSocketException: The 'Connection' header value 'Upgrade,Keep-Alive' is invalid.

     
    Friday, November 8, 2019 1:54 PM
  • Hi Bellisarius,

    Thanks for your feedback.

    I make a test based on your code, and I get the following exception.

    

    Then I use another useful uri to make a test again, but I get no exception.

    Could you provide more code or more details to help us reproduce your problem?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 11, 2019 9:41 AM
    Moderator
  • As I noted, the biggest part of my problem is understanding the source and cause of the exception I was getting:

    WebSocketException: The 'Connection' header value 'Upgrade,Keep-Alive' is invalid.

    As this is coming from a Microsoft library I thought someone on these forums would be able to tell me.

    What I mostly want to understand is whether this Exception is generated before the message is sent (i.e. I have not generated a correct request), or whether it is the result of a reply.

    The service I am connected to is not mine, it is implemented by a library called gwsocket. I connect to this websocket based server from webrowsers on my PC and mobile devices, and I guess many other people do as well as it is a standard piece of software. I also have a program that connects using a websocket client library in Python.

    I don't do anything special with these clients; I just connect with the default parameters and a uri and they connect.

    So I am tending to think the problem may me one of the following:

    1) WebSocketClient has unconventional or a more sophisticated default setup that does not run out of the box; I have not found many examples of it's use.

    2) The reply from the server is incorrect and all the browsers and libraries I have used do not bother to check and continue regardless.

    3) There is a bug in the WebSocketClient library; having not found examples of people using it that may be the case.


    Tuesday, November 12, 2019 9:35 AM
  • Hi Bellisarius, 

    I find a related reference about your exception.

    Switch to another WebSocket client library

    Hope the discussion can help you.

    Besides, you can report a problem in Developer Community about the bug that may exist in the WebSocketClient library.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 14, 2019 9:14 AM
    Moderator