Server Events Client - Getting rid of the automatically appended string at the end of the URI

强颜欢笑 提交于 2019-12-13 16:17:10

问题


I am new to the Service Stack library and trying to use the Server Events Client. The server I'm working with has two URIs. One for receiving a connection token and one for listening for search requests using the token acquired in the previous call. I use a regular JsonServiceClient with digest authentication to get the token like so:

public const string Baseurl = "http://serverIp:port";

var client = new JsonServiceClient(Baseurl)
{
    UserName = "user",
    Password = "password",
    AlwaysSendBasicAuthHeader = false
};

//ConnectionData has a string token property
var connectionData = client.Get<ConnectionData>("someServices/connectToSomeService");

And then use this token to listen for server events. Like so:

var eventClient =
    new ServerEventsClient($"{Baseurl}/differentUri/retrieveSearchRequests?token={connectionData.Token}")
    {
        OnConnect = Console.WriteLine,
        OnMessage = message => Console.WriteLine(message.Json),
        OnCommand = message => Console.WriteLine(message.Json),
        OnException = WriteLine,
        ServiceClient = client, //same JsonServiceClient from the previous snippet
        EventStreamRequestFilter = request =>
        {
            request.PreAuthenticate = true;
            request.Credentials = new CredentialCache
            {
                {
                    new Uri(Baseurl), "Digest", new NetworkCredential("user", "password")
                }
            };
        }
    };

Console.WriteLine(eventClient.EventStreamUri); // "/event-stream&channels=" is appended at the end

eventClient.Start();

The problem with the above code is that it automatically appends "/event-stream&channels=" at the end of my URI. How do I disable this behavior?

I have tried adding the following class

public class AppHost : AppSelfHostBase
{
    public static void Start()
    {
        new AppHost().Init().Start(Baseurl);
    }

    public AppHost() : base(typeof(AppHost).Name, typeof(AppHost).Assembly)
    {
    }

    public override void Configure(Container container)
    {
        Plugins.Add(new ServerEventsFeature
        {
            StreamPath = string.Empty
        });

        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
        new IAuthProvider[] 
        {
            new DigestAuthProvider()
        }));
    }
}

and called Start on it, before calling the above code, but still no luck.


回答1:


The ServerEventsClient is only for listening to ServiceStack SSE Stream and should only be populated with the BaseUrl of the remote ServiceStack instance, i.e. not the path to the /event-stream or a queryString.

See this previous answer for additional customization available, e.g. you can use ResolveStreamUrl to add a QueryString to the EventStream URL it connects to:

var client = new ServerEventsClient(BaseUrl) {
    ResolveStreamUrl = url => url.AddQueryParam("token", token)
});

If you've modified ServerEventsFeature.StreamPath to point to a different path, e.g:

Plugins.Add(new ServerEventsFeature
{
    StreamPath = "/custom-event-stream"
});

You can change the ServerEventsClient to subscribe to the custom path with:

client.EventStreamPath = client.BaseUri.CombineWith("custom-event-stream");

ResolveStreamUrl + EventStreamPath is available from v5.0.3 that's now available on MyGet.



来源:https://stackoverflow.com/questions/48517232/server-events-client-getting-rid-of-the-automatically-appended-string-at-the-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!