Get custom protocol response - javascript

I was learning about custom protocols for few days, and there's one thing that I don't understand.
I know how to start an app with custom protocol, but My question is,
Is it possible to get apps response and print it in Web Browser using javascript?
For example If I will send request to protocol myapp:// , that will open an app written in C#, and that app will return string "This is response" can to print it in Web Browser?
If so, can you help me to accomplish it?
Thanks in advance.

Internet protocols aren't all about browsers.
mailto: causes an action in an email program (e.g. start a new email)
ftp: causes an action in an FTP program (which might be integrated into a web browser or Windows Explorer)
gopher: (well, that's not really prevalent anymore)
myapp:// will cause your (C#) app to start running. At that point, it can do anything a C# app can do. One thing it could choose to do is create a .html file on disk, and use
Process.Start("file://Path/To/My.html")
to cause the default web browser to open the document it just created.
UPDATE
You can certainly have your myapp:// protocol handler send an update to the web server that hosts the page in question. My assumption here is that the myapp:// handler is running on a client machine, and there is a web server on a different URL http://mydomain.com serving a page that includes a myapp:// reference.
Web server renders a page that includes both a myapp:// URL and Ajax code to periodically query the web server for updates to part of the HTML body.
User clicks the myapp:// URL
Protocol handler runs
Protocol handler sends an update to the web server, e.g. http://mydomain.com?user=joe&result=123
Web server uses ?user=joe&result=123 to update response next time the Ajax callback is initiated
Ajax callback gets updated data for page from web server, updates page.

Related

NodeJS chat server interacting with non-node hosted html page

The node.js chat example is ubiquitous. However, they all serve the static html page with which the chat feature is integrated.
What about an html page that is served via apache, php, .net, or other which interacts with a node.js based chat server. How would this be implemented?
For instance, the html page contains a form used to login. The form's action points to the node server which provides the authentication and message handling. How does this chat server communicate with the client-side when it is not also providing the static html content?
Yes, it's possible. If I understand you correctly, you'll probably want to use this approach:
Run the apache server on a different port from the node.js server, and have the static server serve the chat page. Then, you have two main options for how to get data from the static page to the node.js server: either use XMLHttpRequest directly from the static page with CORS (you'll need it because you're running from different ports, but it's still possible to have CORS allow from different ports on the same domain but nothing else, so it can still be secure), or have an invisible iframe of the node.js page on the static page, and then send data to it with postMessage, and then the iframe (which is on the same port as the node.js server, as it's being served by node.js) will forward the data from the postMessage to the server with XMLHttpRequest
You can also do a proxy, but it won't be as good for this type of situation I think, because if Apache is running the proxy, it completely erases how node.js does well with comet and things, but if you run node.js as the main server proxying to Apache, it would be easier just to do everything with node, so I'm guessing you don't want that
Here is a simple solution I found that works to add socket.io real-time interactivity to any existing html page, even when that page is hosted on another server.
Include a reference to socket.io in the head of the html page. The IP address and port number should be the location of your node.js server that is running socket.io.
<script src="http://xxx.xxx.xxx.xxx:xxxx/socket.io/socket.io.js"></script>
[NOTE: localhost doesn't work, you must use the actual IP address - don't forget the port]
Then, within a script block on the same html page, open a connection to the socket:
<script>
var socket = io.connect('http://xxx.xxx.xxx.xxx:xxxx');
</script>
That's it, you're connected!

Is it possible from the server side to change browsers address bar?

Imagine I have a public display, showing a browser displaying a web page.
Is it possible to send a GET or POST from a mobile device to an HTTP server which triggers some AJAX/pubsub/websocket JavaScript function that changes the page that is presently being viewed on the display, or even just changing an iframe's current domain?
Cross-domain pushstate? Is this at all possible even on your own setup?
Assuming you control the web page being shown on the public display, yes.
The web page would need to either periodically contact the server via AJAX, or have a long-running connection to the server (i.e. Comet or WebSockets).
When the server receives the request from the mobile device, it either uses the Comet connection to send the new URL to the web page, or when the web page next contacts it via AJAX, it sends the new URL in response.
The web page then sets its own window.location property to this new URL.
Note that once it's done this, you'll no longer be able to send the browser in question to another new page, unless the page that you've just sent it to also includes the JavaScript that contacts your server.
But if you don't control the web page being displayed...
Then you'd need a browser extension to initiate the connection between the browser and your server.
You can do this with JavaScript on the client side. This is covered pretty well in that question: Updating address bar with new URL without hash or reloading the page
Unfortunately, it's a relatively new, and mostly unsupported feature. Your alternative is to set the hashtag and use it for navigation.
UPDATE:
If you are trying to "push" pages to the user like a TV channel, then you can have an AJAX request poll the server every few seconds to see if there's a new page. The server would respond with the new URL. You could then put that page in an iFrame.

how to get the data from serial port (RS-232) on client side

My project i'm doing now is getting the weigh from the scale that use RS-232 port and post it into the website then press submit and the data will be saved into the server.
I also study how to get the value from these port using the java API called javax.comm. However, I think it just work on the server, and could be work for one computer. that's the problem. therefore, I want to make a website that the client computer can access to the website and weigh the scale then save it into the server. So, how can we do it? Does javascript work on it?
thanks :)
Of course you amy do this with a browser plugin, but that will make the application browser specific. Instead what you may do it to create a simple desktop agent (windows service, taskbar app) that will be installed on the client machine itself.
Now this agent should respond to HTTP requests from your web page that are directed to http://[localhost]:[port]. This might need to embed a simple HTTP server inside the agent. The other complexity you will have to handle is on Cross Origin Requests. You may either use JSONp OR CORS in handling that.

Android WebView AJAX sandbox

I need to embed WebView in my application, which has to pull some data via AJAX from multiple remote servers. Unfortunetely due to ajax sandbox, connections to foreign servers are blocked. How can I disable it, as the js code I'm running is trusted?
There's a simple workaround to allow connections to single server. It's as simple as using loadDataWithBaseUrl and passing the top level url as the first parameter. But what to do, when js should be able to access multiple different domains?
Thanks
Are the pages loaded into the webview local? i.e, Are they loaded from the local file system like: file://yourpage.html or are they remote pages?
Webpages loaded locally are not affected by the cross-domain ajax restrictions so you can load whatever you like.
If they're remote pages then i'm not sure how you're going to get around it, perhaps setup your own webservice on the same domain as where the pages are served from which simply fetches the data from the remote services and spits it back

can javascript receive socket request?

How can I make my javascript client application receive socket requests?
I mean not response on request, but request itself.
Javascript is running in browser without HTML5.
The thing is that I want my web page to reload changed content but without the need of making request to the server each several minutes.
I hope that a server can make some request to javascript on the page, making it refresh the page. If not what could you suggest instead javascript in this scope.
Many browsers that support HTML5 implement WebSocket interface. WebSocket allows two way communication, so browser and server can send requests. Check this post for more info What browsers support HTML5 WebSocket API?
If your browser doesn't support WebSockets you could try WebSocket emulation written in Flash/JS from this site https://github.com/gimite/web-socket-js
If this is also not suitable for you then the last option is "long polling". In this case browser ask server for some data and if server does not have any information available for the browser it doesn't send empty response. It holds the request and waits for new data to be available. Browser after receiving new data immediately ask server once again.
Check these links for more information:
http://en.wikipedia.org/wiki/Push_technology
http://en.wikipedia.org/wiki/Comet_(programming)
Yes and No.
No
Javascript running in a browser environment can't listen to sockets since it's running in a sandbox with limited capabilities.
Yes
However, JS is a full fledged programming language, so if you have it running in an environment where it is not crippled yes, it can do that and more.
A nice example is node.js - http://nodejs.org/
Wiki page - http://en.wikipedia.org/wiki/JavaScript#Uses_outside_web_pages

Categories