DVLP / signalr-no-jquery

120 stars 77 forks source link

transportLogic.getUrl issue adds redundant host #1

Closed viktorlarsson closed 6 months ago

viktorlarsson commented 7 years ago

When creating a hubConnection, everything works fine but after doing connection.start, the transportlogic.getUrl messes up the url as you can see in the following image.

Screenshot from dev tools

This is fixed in the ms-signalr-client repo by adding connection.appRelativeUrl instead of connection.url https://github.com/dfrencham/ms-signalr-client/blob/master/jquery.signalR.js#L1306

Old snippet (not working): var baseUrl = transport === "webSockets" ? "" : connection.baseUrl, url = baseUrl + connection.url, qs = "transport=" + transport;

New snippet (working) var baseUrl = transport === "webSockets" ? "" : connection.baseUrl, url = baseUrl + connection.appRelativeUrl, // here we go qs = "transport=" + transport;

DVLP commented 7 years ago

Thanks for contribution but this raises another problem related to this bug where Url coming from negotiate call overwrites Url defined during connection https://github.com/SignalR/SignalR/issues/3776

viktorlarsson commented 7 years ago

The fix for it seems to be almost identical to the one that I made on the pull request?

https://github.com/SignalR/SignalR/issues/3776#issuecomment-248584717

DVLP commented 7 years ago

this fix adds this piece of code

//take my url, if not the same!
if (url != connection.url) {
  url = connection.url;
}

which defies the need of having

url = baseUrl + connection.appRelativeUrl

because the url from the server's negotiate call will always be ignored in favour of the one initially set in client

so in shorter version it's just like back to having

url = baseUrl + connection.url
viktorlarsson commented 7 years ago

I am not getting the same results here. Are you hosting your frontend on the same port/server as the backend for the signal r?

My frontend code is located on another port then the API. Frontend: http://localhost:1337. Backend: http://localhost:20529/api/

this.connection = hubConnection('http://localhost:20529/api/', {logging: true});

When performing a negotiate, everything works.

[15:38:42 GMT+0100 (Västeuropa, normaltid)] SignalR: Auto detected cross domain url. signalR.js:86 [15:38:42 GMT+0100 (Västeuropa, normaltid)] SignalR: Using jsonp because this browser doesn't support CORS. signalR.js:86 [15:38:42 GMT+0100 (Västeuropa, normaltid)] SignalR: Client subscribed to hub 'readmodelhub'. signalR.js:86 [15:38:42 GMT+0100 (Västeuropa, normaltid)] SignalR: Negotiating with 'http://localhost:20529/api//signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22readmodelhub%22%7D%5D'.

But when trying to start the long polling transport, the following happends.

[15:38:54 GMT+0100 (Västeuropa, normaltid)] SignalR: longPolling transport starting. signalR.js:86 [15:38:54 GMT+0100 (Västeuropa, normaltid)] SignalR: Opening long polling request to 'http://localhost:20529http://localhost:20529/api//signalr/connect?transport…eG0sSE4F8qYv9uQ&connectionData=%5B%7B%22name%22%3A%22readmodelhub%22%7D%5D'. jQueryShim.js:69 Uncaught DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL at Function.ajax (http://localhost:1337/vendor.bundle.js:58194:11) at Object.ajax (http://localhost:1337/vendor.bundle.js:28619:22) at poll (http://localhost:1337/vendor.bundle.js:29817:55) at http://localhost:1337/vendor.bundle.js:29945:18 ajax @ jQueryShim.js:69 ajax @ signalR.js:1167 poll @ signalR.js:2365 (anonymous) @ signalR.js:2493 signalR.js:86 [15:39:02 GMT+0100 (Västeuropa, normaltid)] SignalR: longPolling transport timed out when trying to connect. signalR.js:86 [15:39:02 GMT+0100 (Västeuropa, normaltid)] SignalR: longPolling transport failed to connect. Attempting to fall back.

As you can see, the url becomes strange when using connection.url rather then connection.appRelativeUrl:

http://localhost:20529http://localhost:20529/api//signalr/connect?transport…eG0sSE4F8qYv9uQ&connectionData=%5B%7B%22name%22%3A%22readmodelhub%22%7D%5D

The master branch of ms-signal-r seems to be using appRelativeUrl as well.

juliusl commented 7 years ago

I have the exact same problem as @viktorlarsson , and I have to edit the source file in the same way. (PR #2 )

It might not be a good idea to change too much of the internal implementation considering the documentation in this library denotes the 2.2.1 version of SignalR. This creates undocumented change from the core signalr which makes this package harder to use.

AbrarJahin commented 7 years ago

As far as I know, using

window.location.origin

will solve everything

aclave1 commented 7 years ago

Any update on this?