I have a home automation system which has a small Linux server and an app to control my lighting system. The problem is that this system is too limited for my use, so I wanted to create a desktop app (Java or C#) to send my own data to the server.
When I log into the webpage, I can just use FireFox' JavaScript console to send the commands. But my question is how I can log into a webpage and send JavaScript data to it programmatically.
I also tried sending the data through a WebSocket or TcpClient, but I have no idea how that works.
You just have to perform the same requests than the browser when you are on the website, that is (most probably) HTTP GET and HTTP POST. It is possible to send the request with any programming language, including JavaScript.
Related
I've written a game server in python which communicates to multiple pygame clients via sockets. Since it's a turn-based card game there isnt that much data flowing and it's in JSON format. Because I don't want people having to download the client I've started working on a 3D UI with three.js and it's not looking bad.
However I am currently stuck with the communication between the backend (my python game server) and the frontend (The webpage / client I am building with html, javascript and three.js). I feel like my question is already pretty specific and don't get any helpful results when looking online.
How can the webpage (client) and the python server communicate? What changes do I need to make on the python server side? And what do I need to know to implement this on the client side in javascript?
Because your client is running in a web browser, your main limitation is what the browser supports. Javascript in browsers is severely limited so that a website can't trick computers that visit the website into doing bad stuff.
So the first step is to find out "How can my Javascript web page communicate with a backend at all?" and then once you have chosen something, write the backend after that.
You have two options: HTTP requests (AJAX) or websockets.
An HTTP request is basically the same way the browser downloads the page to begin with, and the pictures on the page. The browser says to the server "hey, give me the address http://127.0.0.1:8000/play_card?card_number=3 and the server says back "the page says OK". Little does the browser know, that your server made you play card number 3 and now it's the next player's turn.
This is one-way - the client can ask for stuff, the server can't just send it. So the client has to keep asking over and over, whether anything has happened. The client might ask for address http://127.0.0.1:8000/game_status?player=5 and maybe the server says {} if nothing has changed. (Or maybe it says the entire game status and then you don't need the player number. Depends on the game.)
The other option is websockets. The browser sends a special request saying "hey please make this connection into a websocket" and the server sends a special response saying "okay, this connection is now a websocket", and then the connection stays open for as long as the client wants, and the client and server can send data at any time. It's not just a normal socket, though, because there are extra security measures to prevent the webpage from doing naughty stuff. And the connection always starts with an HTTP request and response.
In either case, note that http://127.0.0.1:8000 has to be the same website that the web page came from - websites can't just send requests to other websites (that would be naughty), so your server also has to serve the game page itself. (not difficult)
You can implement HTTP and/or websockets yourself by listening on a socket on port 8000 (or any other port), but you'll have to learn the HTTP protocol and get it right or else it won't work. It's actually not too difficult but it is work that you have to do. Or you can use a library someone else wrote, like http.server or websockets.
Is it possible to access blpapi from javascript running in the client's browser?
I wish to access bloomberg's API from javascript running in the client's browser, in the assumption that the client has an open bloomberg session and therefore bbcomm is running.
Conceptually, this would be the same as accessing the blpapi on the client side from python.
However, all existing solutions I found in js appear to be server-side:
blpapi-node (node-based)
blpapi-httm (creates a server where to post http request)
blpapi-react (cannot make this module work)
Now you can access Bloomberg data natively in JavaScript via Web AppPortal. This allows you to write web based applications that run inside LP Components.
To learn more about Web AppPortal, go to MYAP 5
To download the SDK, please type SDK -> select SDK -> AppPortal Web SDK -> click Install.
This is obsolete, see Mourad Barakat's answer above
Conversation with Bloomberg Support confirms this is not possible**
BB say they don't support javascript access, and that one solution is to use their Server API to use the authentication of the client (who has a bb terminal open) to query data and return it to the client
An alternative hack
An alternative hack is to create an executable mini-server that the client downloads and launches, and that offers an http interface to get data to the webapp.
For example, this could be done in Flask in Python, and in fact it has already been done by blpapi-web (excluding the executable part, for which you can use PyInstaller and py2exe for Windows and py2app for Mac)
I am building a java chatting web application.(Server and Client in one project)
follow is my condition
Springframework 4.2.3
JSP
Maven Project which converted from a Dynamic Web Project
Unable to use node.js
So User scenario in my head is
User enters some text and press Send
Ajax call to deliver messages to server
Server checks the users who are currently connected(from Session maybe)
Server calls other users script to append new message
I am quite confusing with step4. Is it possible that Java calls DOM event trigger?
How could a client get a new message event from server?
Thanks. :D
P.S.
These days majority of chatting servers are event-driven. Is it possible to build an event driven chatting server with Java?
Your solution is formed as if there is no WebSocket technology available to you. WebSocket is implemented to solve real time messaging issues. It pushes message to the destination.
But if to stick wih your method following is meaningful.
You need someplace to keep incoming messages such as database or session.setAttribute [bad idea]. Then use some ajax call loop on the clients machine to ask for a new incoming message from server.
Probably your server will slow down due to incoming flood of GET requests from multiple users.
to Your last question in post scriptum: yes, I use tomcat websocket api.jar in my projects. There is well written documentation on apache.org
As I mentioned, learn WebSocket if your users are not using old internet explorer browsers. There are bunch of tutorials on it...
I'm new to node.js
To illustrate my question, I'll use an example:
Client sends a command via telnet/tcp to socket server (ie 'telnet 192.168.0.10 8888' then 'showDiv1')
Command is forwarded from server to a Web page
depending on command, different are displayed on the web page
There are plenty of examples on the node.js website, but it seems difficult to combine the sample scripts to produce the desired functionality above. Any ideas?
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