As part of my third year project for university I am creating a HTML5 Web App for the amazon fire tv OS. This web app is supposed to communicate with another remote server to update another client with information about what the user is currently watching. I have succesfully implemented this communication using socket.io on localhost but unfortunately the android OS does not support web sockets, so I cannot use socket.io within the fire tv app itself.
To get around this I thought about having a php file on my server that will emit the socket request when a relevant GET request for the page is requested. The php code for the file is below:
if(isset($_GET['title'])){
echo "
socket.emit('vidStart', '" . $_GET['title'] . "');
";
}
echo "</script>";
As you can see all the code does is emit when the page is requested using a title parameter.
Now, this works fine when I go to the web browser myself and enter the url, the emit succeeds and my server receives the message.
However, I am trying to execute this on the server remotely using javascript, to get around having to use socket.io in my fire os app.
The code I currently have in my app is below:
$.ajax({
'type': 'GET',
'url': 'http://138.68.175.172:81',
'data': 'title=example',
success: function(response){
console.log(response);
}
});
This returns the correct response but doesnt seem to actually execute the php code and emit via socket io on my server. My question is, is this possible to do via javascript and php? I have tried looking at XMLWebRequest but it seems to do the same thing, and responds with the web page.
If this isn't possible, is there any other way to send information to a remote server, and have it execute a command ( in this case emit a socket.io message)?
Related
I have a node server sitting between my java monolith (backend) and browser. the node server handles the sever side rendering of react components and all.
I am using superagent-bluebird-promise to handle xhr on the node server. Now, the node server also hits the backend's apis and the backend can in some cases redirect an xhr call with a 30x to another URL.
Here i want to handle this. i dont want the node server to hit that redirect, but i want to catch that, and do the redirection based on some conditions.
Is that possible to handle? If yes, how?
If not, what would be the best way to handle backend redirects on the node server?
You can set redirects to 0, so the error will be thrown on first redirect. Something like this:
request.get(url)
.redirects(0)
.then(res=>console.log({res}))
.catch(err=>console.log("Redirected to: " + err.originalError.response.headers.location));
Im trying to uses this function but only print a message in console not redirect the actual url of my server when file is changed
watcher.add("/home/diegonode/Desktop/ExpressCart-master/routes/2.mk");
watcher.on('change', function (info) {
console.log(info);
router.get('*',function(req,res){
res.redirect('http://www.google.com'+req.url)
})
});
please help me
If you want the user who is on your page to get redirected when the file changes on the server then you will not be able to do that using only server-side code. You need to send a message to the client, using e.g. WebSocket, Socket.io, long polling etc. and the client needs to react to that message and do the actual redirection (i.e. navigate to some URL).
I am using openfire as an XMPP server and using converse as client library. I want to send a chat message from my chat window to openfire. For this I want to send the text to a converse method which will send the message to the XMPP server.
I am trying to send the message using the following:
var msg = converse.env.$msg({
from: 'a1#localhost',
to: 'a6#localhost',
type: 'chat',
body: "Hi"
});
converse.send(msg);
But this sends the following frame in network of console in websocket:
message from='a1#localhost' to='a6#localhost' type='chat' body='Hi' xmlns='jabber:client'/>
This does not transfer message to the other user neither it stores it in the table. I am pretty much sure I am calling a wrong function. Can anyone povide any help.
You are calling the right function.
What you'll probably miss:
Listener of messages in "a6#localhost" client: as I read in documentation there are few functions
Probably, the right name of server. "localhost" has problem. You can
check Openfire for real service name on his own web panel
To check if a message it's delivered in Openfire you'll can check
OF's log (check debug one, but probably you'll have to enable it).
Real time messages are not stored on database, only groupchat's ones
and not everytime AND offline messages. To not find them on db means nothing
https://conversejs.org/docs/html/development.html
converse.chats.open('buddy#example.com');
converse.chats.get('buddy#example.com');
converse.listen.on('message', function (event, messageXML) { ... });
The syntax is wrong. conversejs uses strophe plugin to construct and send messages. It exposes the strophe $msg message builder for constructing stanzas. It has to be in the following format:
converse.env.$msg({from: 'a1#localhost', to: 'a6#localhost', type: 'chat'}).c('body').t('Hi');
You need to add a body node and within it a text node for the message.
You can also create and add your own api method and internally create a method that sends your custom stanza, and expose it using the api.
I am new at web developing and I want to do webpage for remote controlling my Raspberry PI. On raspberry I have few sensors attached and I can get data by sending request on 192.168.1.100:9997. There is code written in Python. Everything works if I try to get data with Putty for example. Now I want to establish TCP connection for reading data over my webpage. I was searching for few days and found that this is possible by creating Websockets. There are many tools, most described I found is Node.js. As I understand with Node.js is possible to create Websockets and it can also serves webpage (instead of Appache for example)?
For example I am running this Websocket server just for reading data from RPi in "server.js". Now I don't know how can I get this data from "server.js" into my .html? I didn't find any very basic examples. I can get data via database, but this is not what I want. I also want to send request from my Webpage to Rpi and then read the answer.
I hope you understand my problem. If you can point me in some good examples or tell me how it must be done I will be very glad. I want to do this with Javasrcipt if it's possible.
Thank you in advance.
EDIT: I have now working example with Node.js, but I don't know how to implement this into my Web page that user can trigger this part of codes from .html, and show answered data into .html web page. I hope this helps.
var client = new net.Socket();
client.connect(9997, '192.168.1.100', function() {
console.log('Connected');
//sending request
//THIS SHOULD BE TRIGGERED FROM HTML onclick for example
client.write('$DATA');
});
client.on('data', function(data) {
console.log('Received: ' + data);
//THIS DATA SHOULD BE SHOWN IN HTML for example
//client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
For getting data off a Pi and into a Web page, take a look at some examples doing this using WAMP (an open protocol which runs on top of WebSocket) and Crossbar.io (open source router for WAMP) - http://crossbar.io/iotcookbook/Raspberry-Pi/
Full disclosure: I'm working on these projects - but they are open source, and a great fit for what the OP wants to do.
I am working on a personal project that has a chat message system. I want the messages to update real time for both users when a new message is sent (like facebook chat). Although I have tried multiple ways to detect changes on the database and AJAX my PHP script that SELECTs the messages I can only update the sender's message in real time. The receiver can't see the new message unless they refresh their page. So I decided to use websockets on my local environment to keep the connection open and AJAX my messages.php script when there is a change on the database. However, when I create the websocket object:
var msgsSocket = new WebSocket("ws://www.localhost:8888/messages.php");
I am getting an error:
WebSocket connection to 'ws://www.localhost:8888/messages.php' failed: Error during WebSocket handshake: Unexpected response code: 200
I also tested with the following URL strings:
www.localhost/messages.php
localhost:8888/messages.php
I am not sure what I am doing wrong. Any help is welcome.
P.S. I am using MAMP