krambriw wrote:Hi, I am not sure what is your problem. I think the code you wrote is somehow functioning. I made a very simple test-page, maybe you can try it and then explain what you would like to change?
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<title>Simple websocket test page</title>
<script language="javascript">
wSocket = null;
prot = "";
ip = "";
url = "";
recent_downloaded_shows = [];
function init() {
// event handlers for WebSocket:
wSocket = null;
prot = "";
ip = location.host;
if (location.protocol == "https:") {
prot = "wss://";
}
else {
prot = "ws://";
}
url = prot + ip; //enough for Webserver
//url = prot + ip + "/ws"; //mandatory for Tornado
wSocket = new WebSocket(url);
wSocket.onopen = function() {
document.getElementById("log").style.backgroundColor ="green"; //Green
}
wSocket.onmessage = wsMessage;
wSocket.onclose = function() {
document.getElementById("log").style.backgroundColor ="#C0C0C0"; //Grey
}
}
function wsMessage() {
recent_downloaded_shows.push(event.data);
if (recent_downloaded_shows.length > 3) {
recent_downloaded_shows[0] = recent_downloaded_shows[1];
recent_downloaded_shows[1] = recent_downloaded_shows[2];
recent_downloaded_shows[2] = event.data;
recent_downloaded_shows.splice(2, 1);
}
document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
}
</script>
<body id="log" onLoad="init()" bgcolor=black text=white link=white vlink=white alink=white </body>
Hello Krambriw,
Sorry for the late reply, I have been really busy with studies lately, thankfully done now.
First of all, I don't get your code to work, think I found out the problem tho'.
Well, we have talked about it before, but what I am trying to do is that I get events like this for example:
Code: Select all
PushBullet.Note.DownloadFinished [u'White Collar - 5x11 - Shot Through the Heart', None, u'ujAdLjXAeDksjAdxAd0gbA', '', '2015-10-22 01:17:17']
Then I use
Code: Select all
Webserver.BroadcastMessage(u'{eg.event.payload[0]}', False)
to get the payload to my webpages and I have the code I past last time, this one:
Code: Select all
<script type="text/javascript">
ws = new WebSocket("ws://" + location.host);
recent_downloaded_shows = [];
ws.onopen = function(event) {
console.log("Web Socket opened.");
}
ws.onmessage = function(event) {
recent_downloaded_shows.push(event.data);
if (recent_downloaded_shows.length > 3) {
recent_downloaded_shows[0] = recent_downloaded_shows[1];
recent_downloaded_shows[1] = recent_downloaded_shows[2];
recent_downloaded_shows[2] = event.data;
recent_downloaded_shows.splice(2, 1);
}
document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
}
ws.onclose = function() {
console.log("Web Socket closed.");
}
ws.onerror = function(error) {
console.log("Web Socket error: " + error);
}
Now as you said, this code it works, somewhat okay. But I am trying to add better sorting and adding.
I wish to populate a list or something like that, and make the latest downloaded show to move one down when the next downloaded show is in.
Like this:
- New show (3) (this is the newest show)
- New show (2) (second newest)
- New show (1) (third newest)
-->
- New show (4) (now this is the newest show and get added to the top of the list)
- New show (3) (this gets pushed down one)
- New show (2) (same with this)
new show (1) is now out of the picture.
So, what I am having problems with is proper sorting and adding. And I've taken a look at a simple_page.html you sent me ages ago with that sort of functionality, but I just can't manage to adopt it to my needs. The code I have now basically works fine and it's no bother of me, but I'd just love to be able to add some sort of logic to this, if you understand what I mean.
Edit: I am not locked down to HTML, can do this in python as well I guess, I just don't know how yet. Because as I have figured this will also include every other update broadcast I do, so if I for example do a "Broadcast_message: TV IS ON" - TV IS ON will also be added to the list.