1388-2/HB said:
Is JavaScript able to send/listen for data on a specific port? I'm
seeking a solution to real time data interaction with my web server that
doesn't
require refreshing the page. I.e., a chat room, where the data can be
broadcast from the server arbitrarily and displayed by the client
browser(s). To accomplish the data transmission can I use JS, or do I
need to augment my client-side platform to something like Java, et al?
Hi,
Javascript is not suited/able to directly open sockets and such actions.
However, you can relatively easy accomplish the same functionality by
incorperating a server into your application.
eg: JS can use a hidden frame to load/read info that comes from a server.
You can also use the XMLHTTPRequest-object to circumvent the hidden frame.
You need a serverside scriptinglanguage (like PHP) to produce information
you want.
A rude setup would look something like this:
2 frames, 1 hidden. (named displayFrame and communicationFrame)
Let JS load another page in the hidden communicationFrame: getInfo.php
This page contains the following info:
<html>
<body onLoad="sendInfo();">
<script type="text/javascript">
function sendInfo(){
// The following info is produced dynamically on the server
parent.frames.displayframe.receiveInfo("This text comes from some
database or from some chatengine.");
}
</script>
</body>
</html>
and in the displayframe you have a function receiveInfo that receives a
string and does something with that.
Now write some logic that refreshes the hidden frame every 10 seconds or
something like that.
I wrote application like this, it is fun, but the responsiveness is much
lower than can be achieved using an applet and Java, because of all the
overhead and loading.
Anyway: I hope this gets you going.
Good luck!
Reagrds,
Erwin Moller