By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

javascript and server socket

P: n/a
hi all,

I'd like to implement a server socket in java: something linke the
following example.

The problem is that the HTML has not the permission to execute
instruction
serverSocket =
Components.classes["@mozilla.org/network/server-socket;1"].
createInstance(Components.interfaces.nsIServerSock et);

Question:
1 - how do I give it this permisison?

2 - one solution I thought was to put the javascript code in a firefox
extension, to load, so it should have all needed permisison, but,
How can I call a java script function define in an extension
from an html page? - there's some particular syntax?

thanks

giammy

<<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<script>
var serverSocket;

function start()
{
var listener =
{
onSocketAccepted : function(socket, transport)
{
try {
var outputString = "HTTP/1.1 200 OK\n" +
"Content-type: text/plain\n\n" +
"Hello there " + transport.host + "\n";
var stream = transport.openOutputStream(0,0,0);
stream.write(outputString,outputString.length);
stream.close();
} catch(ex2){ dump("::"+ex2); }
},

onStopListening : function(socket, status){}
};

try {
serverSocket =
Components.classes["@mozilla.org/network/server-socket;1"].
createInstance(Components.interfaces.nsIServerSock et);

document.getElementById("log1").value = "got class";

serverSocket.init(7055,false,-1);
document.getElementById("log2").value = "got init";
serverSocket.asyncListen(listener);
document.getElementById("log3").value = "got listener";
} catch(ex){ dump(ex); document.getElementById("log9").value = ex; }

document.getElementById("status").value = "Started!";
}

function stop()
{
if (serverSocket) serverSocket.close();
document.getElementById("status").value = "Stopped!";
}
</script>

</head>

<body>
Verifica apertura socket:
<br>

<form>
<input id="b1" type="button" value="Start" onclick="start();">
<input id="b2" type="button" value="Stop" onclick="stop();">
Status: <input id="status" type="text" value="STOPPED">

<p>

<br><input id="log1" type="text" value="">
<br><input id="log2" type="text" value="">
<br><input id="log3" type="text" value="">
<br><input id="log4" type="text" value="">
<br><input id="log5" type="text" value="">
<br><input id="log6" type="text" value="">
<br><input id="log7" type="text" value="">
<br><input id="log8" type="text" value="">
<br><input id="log9" type="text" value="">

</form>
<br>
<br>

</body>
</html>

Feb 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
giangiammy wrote:
I'd like to implement a server socket in java:
Java or JavaScript? Because they have only the first four letters in
common. <URL:http://jibbering.com/faq/>
something linke the following example.

The problem is that the HTML has not the permission to execute
instruction
HTML does not execute anything. It is a _markup_ language.
serverSocket =
Components.classes["@mozilla.org/network/server-socket;1"].
createInstance(Components.interfaces.nsIServerSock et);
This is script code, executed by the JavaScript engine.
Question:
1 - how do I give it this permisison?
The script can request permission, and it can (not: must) be given by the
user:

<URL:http://developer.mozilla.org/en/docs/Bypassing_Security_Restrictions_and_Signing_Code>
2 - one solution I thought was to put the javascript code in a firefox
extension, to load, so it should have all needed permisison, but,
How can I call a java script function define in an extension ^^^^^^^^^^^
Again, the language is named "JavaScript" for a reason.
from an html page? - there's some particular syntax?
[...]
AFAIK, you cannot. If you could, this would allow any foreign Web site
to trigger your extensions which do not run in the sandbox like the
client-side script code of the Web site. Surely you do not want that
to happen.
[invalid markup]


Learn HTML before you learn J(ava)Script/ECMAScript.

<URL:http://validator.w3.org/>
PointedEars
Feb 2 '06 #2

P: n/a
hi all,

I made some progress implementing the server socket using
javascript:

I wrote some code: it opens the socket, but I get the error
"Permission denied to get property UnnamedClass.host"
in the shown line, when I try to connect.
I think I need some instruction like:
netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
(but this is not working: any idea what privilege I need to
give?)

thanks again
giammy

var serverSocket;

function start()
{
var listener =
{
onSocketAccepted : function(socket, transport)
{
try {
var outputString = "HTTP/1.1 200 OK\n" +
"Content-type: text/plain\n\n" +
"Hello there " + transport.host + "\n";
// THIS FUNCTION FAIL FOR PERMISSION PROBLEMS
var stream = transport.openOutputStream(0,0,0);

stream.write(outputString,outputString.length);
stream.close();
} catch(ex2){ dump("::"+ex2); }
},

onStopListening : function(socket, status){}
};

try {
netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
serverSocket =
Components.classes["@mozilla.org/network/server-socket;1"].
createInstance(Components.interfaces.nsIServerSock et);
serverSocket.init(7055,false,-1);
serverSocket.asyncListen(listener);
} catch(ex){ dump(ex); }

document.getElementById("status").value = "Started!";
}

function stop()
{
if (serverSocket) serverSocket.close();
document.getElementById("status").value = "Stopped!";
}

Feb 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.