Web PubSub Connect fails when using FWebSocketsModule library and GenerateClientAccessUri in WebPubSub SDK

Greg Tinney 121 Reputation points
2021-08-24T18:51:44.573+00:00

Hi,

I'm trying to implement a WebPubSub client in a video game using UE4 game engine's FWebSocketsModule C++ class, which links with libWebSockets library.

When I use a "Client Access URL" from Azure WebPubSub Dashboard's "Client URL Generator", and open the WebSocket with

       FWebSocketsModule::Get().CreateWebSocket(PubSubURL, TEXT(""))  

everything works fine. The game code successfully connects to the WebSocket and can receive PubSub events a subscriber on the hub.

126059-image.png

However, when I don't use this "Client URL Generator" devtool to generate the WebSocket url, but instead generate it using the WebPubSub SDK to in a C# app:

      var serviceClient = new WebPubSubServiceClient(Connections.ConnectionString, TEXT("Hub"));  
      var connectURL = serviceClient.GenerateClientAccessUri(userId: "player1234");  
      return connectURL.ToString();  

where Connections.ConnectionString is the "ConnectString" provided in the WebPubSub dashboard, then my game's

      FWebSocketsModule::Get().CreateWebSocket(PubSubURL, TEXT(""))  

operation fails. Since FWebSocketsModule uses a binary library, I don't get complete insight into the failure cause, though I do know I receive a "401 Unauthorized" response from the WebSocket server, so I assume the WebSocket upgrade failed. Unfortunately, since the Azure PubSub service uses wss/secured WebSockets, I can't use WireShark to review the WebSocket upgrade handshaking.

Further confounding the issue, however, is that, if I use that exact same PubSubUrl I generated to open a connection from a C# using the ClientWebSocket() object, the WebSocket connection succeeds, and the PubSub functionality works properly.

        using (var client = new WebsocketClient(url, () =>  
        {  
            var inner = new ClientWebSocket();  
            return inner;  
        }))  

Summary:

  1. If I use the PubSub dashboards "Client URL Generator" to get a URL, the FWebSocketsModule websocket library connects to Azure WebPubSub and works properly

2) If I instead generate the websocket URL using WebPubSubServiceClient::GenerateClientAccessUri() per microsoft samples, the FWebSocketsModule websocket fails with "401 Unauthorized" error.

3) However, that exact same websocket URL works properly when used in a C# program (based on Microsoft samples) and WebsocketClient object.

Anyone have an idea what I'm doing wrong?

Thanks!
-greg

Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
64 questions
{count} votes

Accepted answer
  1. Greg Tinney 121 Reputation points
    2021-08-25T05:11:33.773+00:00

    Argh, finally found the cause of my WebSocket connect issue.

    Basically, it appears that WebPubSub embeds some setup metadata into the derived websocket URL. In particular, adding the userId, even when short, results in 20-30 extra characters in the URL versus when userId is not defined in the call to

         WebPubSubServiceClient::GenerateClientAccessUri()
    

    When I noticed, this I looked at Epic's Unreal 4 Engine source for the wrapper that manages the third-party libwebsockets library. And found this:

     void FLwsWebSocket::ConnectInternal(struct lws_context &LwsContext)
     {
          ...
          **char UrlPath[300];**
          ...
    

    Ugh.

    Bad bug on Epic's part there. For a quick test upped that char array to 500 slots and WebPubSub works great, even with userId and session expirations defined. Though the number of roles defined need to be limited or that 500 characters would not cover it.

    Need to make that string dynamically sized, and should be good to go!

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful