Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: dpd script can change its url base #437

Closed
wants to merge 1 commit into from

Conversation

iranreyes
Copy link

Modification

In my current project I am using Deployd as server of test (like a mock of the remote server). I communicate remote with the Deployd server through HTTP, but to have more control about the data I decided include the dpd.js. So, to include the script I must reference the script in my html like:

<script src="http://localhost:2403/dpd.js"></script>

The problem if that if my server run on another port the dpd script try to recover data from the port of my app instead of the port where it run, in this case 2403.

To Improve the script I add a function that let you configure the BaseUrl, the default behavior is keep it and you can change it calling this function at the begin of your application like this:

dpd.setupBaseUrl(protocol, hostname, port)

If you omit protocol will be used window.location.protocol, if you omit hostname will be used window.location.hostname and port = window.location.port

In my real case I change it like this:

dpd.setupBaseUrl(null, null, 2403)

@@ -13,6 +12,17 @@
var socket = io.connect(root);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.connect(root) run during loading of dpd.js

dpd.setupBaseUrl(null, null, 2403) can only run after that.

Then, io.connect(root) will connect to the default port instead of 2403?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,
For example, I am using AngularJS, when my app is ready to start, execute dpd.setupBaseUrl and pass the port that is located how resource of the app to the method. After this, dpd._ajax will use the new root and the data will be recovered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ajax and socket.io connect to different ports, I think it will cause problems.

Or we can have another function to set protocol and host for socket.io?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this
socketio/socket.io-client#251

socket.io disconnect and connect to another host/server is not that easy.

So, we should run var socket = io.connect(root); once only.

And right now, we auto run var socket = io.connect(root); during including of dpd.js

Either

<script>
window._dpd = {root:'http://localhost:2403'};
</script>
<script src="dpd.js"></script>

Or

<script src="dpd.js"></script>
<script>
// all users should run this, even they use default port.
dpd.init({url:'http://localhost:2403'});
</script>

please comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dpd.init(...) is not working :S now, and dpd.setupBaseUrl(..) neither

@ericfong
Copy link
Member

ericfong commented Dec 9, 2014

How about

  var root;
  if (_dpd.root) {
    root = _dpd.root;
  } else {
    root = window.location.protocol + '//' + window.location.hostname;
    if (window.location.port !== '') {
      root += ':' + window.location.port;
    }
  }

and

window._dpd = {root:'http://localhost:2403'};

before

<script src="dpd.js"></script>

@iranreyes
Copy link
Author

Nice Eric, thanks for your answer but I don't like expose root in that way; because of that I tried to expose a public function in dpd.

@ericfong
Copy link
Member

I don't like that too. Just want to align the host that ajax and socket.io connect to.

@iranreyes
Copy link
Author

Hi eric, I have been a little busy these days. The first days of January I will check it

@andreialecu
Copy link
Contributor

@iranreyes since you're using angularjs, have a look at https://github.com/slively/dpd-angular-cache. I had the same requirement as you and sent a PR to the maintainer to include this functionality (and more).

@ericfong
Copy link
Member

ericfong commented Jan 7, 2015

Actually, I also need this feature for my projects.

I would like to base on your PR or submit another PR for this

@NicolasRitouet NicolasRitouet changed the title dpd script can change its url base Feature: dpd script can change its url base Jan 9, 2015
@andreialecu
Copy link
Contributor

To solve the problem about connecting to socket.io multiple times, the connection could be delayed until the first call to dpd.on dpd.off or dpd.socketReady. That way you can call dpd.setupBaseUrl()before setting up the handlers later on in the code and there should be no issue

This is good because it will also disable connecting to socket.io if the consumer doesn't even use socket.io in the first place and doesn't need it.

@andreialecu
Copy link
Contributor

Here's a gist of the modifications I was talking about. All tests pass.

https://gist.github.com/andreialecu/45eba279e4ccc009a5af

@ericfong
Copy link
Member

Good gist.

Shall we have:

dpd.setBaseUrl(path) {}
dpd.getBaseUrl(path) {}

instead of

function setupBaseUrl(protocol, hostname, port) {}

?

@andreialecu
Copy link
Contributor

I think it's better if the parts of the URL are separated, so you can just override the protocol, or port, but keep the hostname. Say you have your website at http://mydomain.com and the api at https://mydomain.com:1337.

EDIT: However, it could have this signature:

setBaseUrl(options)

Where options is either an object:
{ protocol: X, host: Y, port: Z, path: P }

Or a string in which case it is used as it is.

@andreialecu
Copy link
Contributor

I updated the gist, relevant code:

  function setupBaseUrl(options) {
    options = options || {};
    if (typeof options === "string") {
      root = options;
      return;
    }

    var protocol = options.protocol || window.location.protocol;
    var hostname = options.hostname || window.location.hostname;
    var port = options.port || window.location.port;

    root = protocol + '//' + hostname;
    if (port !== '') {
      root += ':' + port;
    }
  }

@ericfong
Copy link
Member

Let's continue in #490

@NicolasRitouet
Copy link
Member

#490 is the followup of this features, among others.
I'll close this PR.
Thanks @iranreyes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants