Tuesday, March 22, 2011

xmlsockets in Chumby Widget

This took me a while to figure out. I don't think there are a lot of people using ActionScript xmlsockets in a Chumby widget to talk directly with the Chumby - they are usually hitting external websites.

If you want to talk to a python program running on the Chumby itself, you need something like:

from socket import *
s
=socket(AF_INET,SOCK_STREAM)
s.bind((
"localhost",6666))
s.listen(
1)
policyFile
= '<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"><!-- Policy file for xmlsocket://socks.mysite.com --><cross-domain-policy><allow-access-from domain="*" to-ports="6666" /></cross-domain-policy>'
while 1:
print "waiting for connection..."
conn,addr
=s.accept()
print "connection made"
data
=conn.recv(2000)
print "data: ",data
if("policy-file-request" in data):
conn.send(policyFile)
conn.close()
print "connection closed."

Note the weird XML. You to send that back to the Flash Player in response to a policy-file-request to assure the Player that you have the right to access that socket. After that, your ActionScript can talk freely using xmlsockets with the python program running on the Chumby.

5 comments:

OLUWAFEMI said...

Thanks a lot for your post. I tried doing something similar, but I couldn't get it to work. I have a C# application generating random numbers which I proceed to plot on AS2. This seems to work fine on my desktop, however, I can't get it to work on the chumby even after sending the XML to the flash player as suggested by your post. It seems to be connecting, but the OnXML event handler isn't getting fired. Please do you have any suggestions as to how I can solve this problem?

Thanks.

OLUWAFEMI said...

Thanks a lot for your post. I tried doing something similar, but I couldn't get it to work. I have a C# application generating random numbers which I proceed to plot on AS2. This seems to work fine on my desktop, however, I can't get it to work on the chumby even after sending the XML to the flash player as suggested by your post. It seems to be connecting, but the OnXML event handler isn't getting fired. Please do you have any suggestions as to how I can solve this problem?

Thanks.

t11s said...

OLUWAFEMI, when you first try to talk from C# on your PC to the AS2 on the Chumby, are you seeing a response from the Chumby that includes the string "policy-file-request"?

You might also want to look into using "System.security.loadPolicyFile" in your widget to specifically name the IP of your PC.

oluwafemi said...

Thank you for your response. I read up on the "System.security.loadPolicyFile" function and I am calling it within the event handler of the onConnect event. I am however still not getting the widget to plot the data points. Could it be that I am making the call to "System.security.loadPolicyFile" at the wrong place? By the way both applications reside on chumby.

oluwafemi said...

Thank you very much for your help. I finally got it to work with the security.loadPolicyFile call.