...

View Full Version : Javascript Socket Question



roystonmarshall
10-10-2011, 02:17 AM
Hi.
This is my first post, so please bear with me!.
I am writing some Javascript for some InDesign automation (shouldn't matter what for, but some background info). I have up to now been resorting to passing a script to Applescript, which then passed a shell command (via do script) to Terminal.
This works, but is limiting my script to Mac only.
I recently found I can open a socket, and send the PHP page my variables without leaving JavaScript..

Here is the code I am using with the Applescript combo:
function combinedErrorReport(errorText) //errorText is something like "Library not found"
{
myServerAddress = "http://automation.yappp.net/";
errorText = encodeURI(errorText);
myFullScriptString = "do shell script \"curl -0 "+ myServerAddress + "sendSrcError.php?e=" + errorText + "\"";
myForceError = app.doScript(myFullScriptString, ScriptLanguage.APPLESCRIPT_LANGUAGE);
}

and this is what I want to use using a javascript socket...
function combinedErrorReportWithSocket(errorText) //errorText is something like "Library not found"
{
myServerAddress = "automation.yappp.net:80";
errorText = encodeURI(errorText);
conn = new Socket();
conn.open(myServerAddress);
conn.write("GET /sendSrcError.php?e=" + errorText+"\ HTTP/1.0\n\n");
reply = conn.read(999999);
conn.close;
}

The socket option simply does nothing. It opens the connection, but doesn't pass anything to the PHP.

Anyone have any ideas?

Cheers

Roy

Old Pedant
10-10-2011, 03:10 AM
If you have a space or various other special characters in your message, it would make sense that you wouldn't get the full data. You should surely do something like

conn.write("GET /sendSrcError.php?e=" + escape(errorText) + "\ HTTP/1.0\n\n");

You might even need to use encodeUriComponent instead of just escape.

And not to ask a dumb question, but where did you find a Socket library? One that works crossbrowser, at least?

Old Pedant
10-10-2011, 03:11 AM
Oh...and I *assume* that the web page in question is *NOT* in the domain "/automation.yappp.net", yes? Else there's no need for any of this.

roystonmarshall
10-10-2011, 04:26 AM
Hi.

Thanks for the reply.

I am encoding the error:
"errorText = encodeURI(errorText);"
so there are no spaces, or other non alpha-numeric characters being passed.

Also, I am using JavaScript in conjunction with Adobe InDesign, using Adobes ESTK. this is NOT web based, purely desktop javascript, but in this instance communicating with the PHP page on my server.

I am not a web dev, and my JS knowledge only stretches a little way outside Adobes ESTK, but I have tried the Adobe forums with no luck.

Thanks for the reply, maybe you have other suggestions?

Roy

Old Pedant
10-10-2011, 07:57 AM
Oh, that makes sense. I knew Adobe provided a Socket object (since some of the articles you see on using sockets from web JS tell you to do it indirectly via Adobe), so yeah, it sounds like the right approach.

But no, I don't know the proper methods to call, etc., with that Socket object.

No help in the Adobe forums??

roystonmarshall
10-10-2011, 08:09 AM
There is a guy who has been helpful in the Adobe Forum, but he has been quiete recently! Will re-post if I need to.
The weird thing is there have been some log entries to my web page after posting the code on this forum, so someone has managed to use it somehow!

I have tried to contact the person who did it, but still waiting. (daveyerwin)

Out of interest the site I am trying to get my data to is:
http://automation.yappp.net/sendSrcError.php?p=logs

I dont want to give up, but I may have to :(

Thanks anyway,

Roy

DaveyErwin
10-10-2011, 12:18 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta name="generator" content="daveyerwin">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>SJax</title>



<script type="text/javascript">

req=new XMLHttpRequest();
req.open("GET","http://automation.*****.net/sendSrcError.php?e=hello from daveyerwin",false);
req.send(null);
alert(req.responseText);

</script>

</head>
<body>
<div id="container">
<p>hiyas</p>
</div>
</body>
</html>

roystonmarshall
10-10-2011, 07:33 PM
Hi Davey.

Thanks for the reply.

I have tried your suggestion in the ESTK environment, but unfortunately the Adobe library does not support the method you are using (XMLHttpRequest). :(

I will post your snippet back in the Adobe forum to see if someone there can give me a supported alternative.

Thanks Again

Roy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum