Websocket Plugin

Do you have questions about writing plugins or scripts in Python? Meet the coders here.

Re: Websocket Plugin

Postby cfull1 » Sun Sep 04, 2011 3:36 pm

Looks good. How do you get the weather?
cfull1
Experienced User
 
Posts: 101
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Postby krambriw » Sun Sep 04, 2011 6:14 pm

Hi,

The weather data is originating from Google weather. I used a part of the code in my Suntracker plugin and the pywapi.py. You can look into that code how it works.
I have then created a new plugin, specialised for control of my awnings. This plugin generates an event every minute with the current weather condition as payload, I capture this event with another plugin that makes the data reading persistent and sends it to my html page using your websocket plugin.

Best regards, Walter
krambriw
Plugin Developer
 
Posts: 1150
Joined: Sat Jun 30, 2007 2:51 pm

Re: Websocket Plugin

Postby krambriw » Tue Oct 18, 2011 3:45 pm

Hi there, I have big problems connecting to the websocket server with browsers. Currently only Opera is working.
Firefox is doing nothing, chrome and black hawk connects but disconnects right away.

There are some findings on the net mentioning similar problems and also some new websocket version

http://stackoverflow.com/questions/6800190/chrome-disconnects-after-websockets-handshake

Below I have different results from EG log.

Best regards, Walter

For Chrome:
Code: Select all
17:23:12   New Client Connection 1340
17:23:12   Client ready for reading 1340
17:23:12   Begin handshake: GET / HTTP/1.1
17:23:12   Upgrade: websocket
17:23:12   Connection: Upgrade
17:23:12   Host: 127.0.0.1:1235
17:23:12   Sec-WebSocket-Origin: null
17:23:12   Sec-WebSocket-Key: P9hNSuORwDWPOo8JJ150XA==
17:23:12   Sec-WebSocket-Version: 8
17:23:12   Not using challenge + response
17:23:12   Handshake successful
17:23:12   WebSocket.ClientConnected
17:23:12   Client ready for reading 1340
17:23:12   Closing Client 1340
17:23:12   WebSocket.ClientClosed


For Opera:
Code: Select all
17:23:41   New Client Connection 768
17:23:41   Client ready for reading 768
17:23:41   Begin handshake: GET / HTTP/1.1
17:23:41   Upgrade: WebSocket
17:23:41   Sec-WebSocket-Key1: JX1 U20>45aY 04 (M  [Y16b5
17:23:41   Host: 127.0.0.1:1235
17:23:41   Origin: null
17:23:41   Connection: Upgrade
17:23:41   Sec-WebSocket-Key2: .      S G+3K=476 4656  40
17:23:41   Using challenge + response
17:23:41   Handshake successful
17:23:41   WebSocket.ClientConnected
krambriw
Plugin Developer
 
Posts: 1150
Joined: Sat Jun 30, 2007 2:51 pm

Re: Websocket Plugin

Postby Pako » Tue Oct 18, 2011 4:41 pm

Please try Websocket suite plugin (for me worked only Chrome and BlackHawk).

Pako

I moved last posts to the correct topic Websocket suite.
Pako
User avatar
Pako
Plugin Developer
 
Posts: 1283
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic

Re: Websocket Plugin

Postby Siutsch » Sat Apr 14, 2012 8:44 am

@cfull1

I tried your websocket plugin, but it don't work for me.
I use Win7x64 with Firefox11 and an Android Tablet (3.2) with Android Browser, FF11 and Opera Mobile.

Only with FF11 on my Laptop I get access to EG, but with following message:

New Client Connection xxxx
Not using challenge + response
Closing Client xxxx

If I test your client.html with Android I get nothing (Android Browser, FF11 and Opera Mobile).

I tried Websocket suite from pako too, that's too complicated for me. :roll:

Siutsch.
Siutsch
 
Posts: 43
Joined: Wed Apr 28, 2010 6:53 am

Re: Websocket Plugin

Postby cfull1 » Sat Apr 14, 2012 8:10 pm

Hey Siutsch,

A couple things, Android and Opera don't support native websockets. Chrome, Firefox and iOS do (apparenty BlackHawk works too, I've never used it).

The other thing is as soon as Pako released his version of the plugin I stopped working on mine. He is a more experienced coder than I am, especially with Python.

His can be a bit confusing at first, but it's actually not too bad to get the basics going.
Here's how to get basic broadcasting to work:
1) Add the plugin
2) In your autostart macro, add a start server action.
3) Set server title such as MyWebSocketServer
4) I specific an IP address not an Interface
5) Port number is whatever. 1234
5) In Broadcast message just use "Server title", and write MyWebSockServer, and the message.

I hope this helps, let me know if that doesn't work for you.
Sorry about not looking at mine, I just cannot justify trying to fix mine when there's another one there that works, and does the same thing as mine and more.
cfull1
Experienced User
 
Posts: 101
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Postby Siutsch » Sun Apr 15, 2012 3:43 pm

Hi cfull1,

thankyou for your tips.

I tried it and with your client.html (poke) it already works. :)

Now I have to deal with websocket.html from pako which is considerably more complicated for me.

Siutsch.
Siutsch
 
Posts: 43
Joined: Wed Apr 28, 2010 6:53 am

Re: Websocket Plugin

Postby cfull1 » Sun Apr 15, 2012 6:17 pm

Glad to hear it. What are you trying to accomplish?
cfull1
Experienced User
 
Posts: 101
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Postby Siutsch » Mon Apr 16, 2012 6:42 am

(First: sorry for my bad english, I hope you understand my description ...)

I use, besides a Logitech Harmony, also a webinterface with JQuerymobile to control my home theater
(and light, shutters, etc.) with my Laptop, Android Tablet (Acer Iconia A500) or my Smartphone (Galaxy S2).

To get status info from EG I use the dynamic web server plugin from EG.
When I click a button to start an action (for example to start TV-SAT-AMP, etc. as with the logitech remote control),
the side will reload and EC alter some variables, also the refresh variable (<meta http-equiv="refresh" content="5">,
from then the side will reload every 5 seconds till the action is finished, after that EG changes also the refresh variable
to "none" again and relaoding of the side stops.

It's a well-functioning workaround (for me) to get status infos from EG and show them on the webside without having to
reload the side the whole time, reloading is always only activated for the time of execution of actions within EG.

With the possibilities of websocket I hope I could of course solve this much more elegant.
I don't need the whole page to refreh for updated status infos an could get them immediately.
The status infos could thus be even greater in detail (can be updated more frequently than once every few seconds).

What I would have to know is how I can show the contents of variables from EG on the website.

With your client.html example I can only test a button click (poke) that arrives at EG and with message test from eg
I get a popup-window on my pc (so far it works in general).

Now I must look just to show infos directly on the page.

(Don't know if this all could also be done with Ajax (or so), but I'm no programmer and I don't even undestand ajax.)

Siutsch.
Siutsch
 
Posts: 43
Joined: Wed Apr 28, 2010 6:53 am

Re: Websocket Plugin

Postby cfull1 » Tue Apr 17, 2012 2:57 pm

Sounds pretty straight forward. Again I'm not very familiar with Pako's plugin but he has "persistent variables" that you could check out. I've just been broadcasting a message that my client knows how to parse.

Here's how I create the connection on my client:
Code: Select all
   function Websocket() {
      ws = new WebSocket("ws://192.168.1.101:1234"); //Server's IP address
      ws.onopen = function() {
         wsConnection = true;
      };
      ws.onmessage = function (event) {
         messageHandler(event.data);
      };
      ws.onclose = function() {
         wsConnection = false;
      };
   }


Whenever the server broadcasts a message this function is called:
Code: Select all
   function messageHandler(event) {
      message = event.split("|");
         switch (message[0]) {
            case 'Auth':
               break;
            case 'PlaylistShuffle':
               break;
            case 'PlaylistRepeat':
               break;
            case 'Alarms':
               break;
            case 'Wakeup':
               break;
            case 'Reload':
               reload();
               break;
            case 'Alert':
               alert(message[0]);
               break;
         }


An example of what EG broadcasts is this:
Code: Select all
eg.plugins.WebsocketSuite.BroadcastMessage(u'Server', '1234', 'Alert|This is a test', 0)


This is seems to be the most basic way to communicate between the client and EG. Again the methods pako has created may be easier I just haven't used them
cfull1
Experienced User
 
Posts: 101
Joined: Thu Aug 19, 2010 4:52 am

Re: Websocket Plugin

Postby Siutsch » Tue Apr 17, 2012 3:54 pm

Thankyou.

In the meantime, I could enable the communications in general already (similar code).

I was not yet clear just how to convey different information but I already thougt, that this could be done
with "persistent variables".

Your solution (function messageHandler) is also very interesting, I will try it.
(EDIT: One question after trying: how can I show the data on the webpage? Like <div id="Auth"></div> ? )

From what I understand, I can transfer different information, but only one at a time, not several at once,
for that I must then probably take the solution with "persistent variables" from pako.

But so slowly I come into the topic "Websocket".

Future questions I put up better in the Websocket Suite thread from pako.

Again thank you very much! :)

Siutsch.
Siutsch
 
Posts: 43
Joined: Wed Apr 28, 2010 6:53 am

Previous

Return to Coding Corner

Who is online

Users browsing this forum: No registered users and 5 guests