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
Conversation
@@ -13,6 +12,17 @@ | |||
var socket = io.connect(root); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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
How about
and
before
|
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. |
I don't like that too. Just want to align the host that ajax and socket.io connect to. |
Hi eric, I have been a little busy these days. The first days of January I will check it |
@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). |
Actually, I also need this feature for my projects. I would like to base on your PR or submit another PR for this |
To solve the problem about connecting to socket.io multiple times, the connection could be delayed until the first call to 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. |
Here's a gist of the modifications I was talking about. All tests pass. |
Good gist. Shall we have: dpd.setBaseUrl(path) {}
dpd.getBaseUrl(path) {} instead of function setupBaseUrl(protocol, hostname, port) {} ? |
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:
Where options is either an object: Or a string in which case it is used as it is. |
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;
}
} |
Let's continue in #490 |
#490 is the followup of this features, among others. |
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:
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:
If you omit protocol will be used
window.location.protocol
, if you omit hostname will be usedwindow.location.hostname
and port =window.location.port
In my real case I change it like this: