javascript and server socket

G

giangiammy

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.nsIServerSocket);

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.nsIServerSocket);

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>
 
T

Thomas 'PointedEars' Lahn

giangiammy said:
I'd like to implement a server socket in java:

Java or JavaScript? Because they have only the first four letters in
common. said:
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.nsIServerSocket);

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:

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
 
G

giangiammy

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.nsIServerSocket);
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!";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top