我正试图通过Websocket向服务器发送一些消息。该网页包含一个带有javascript的简单html。这是html文件"delete_This.html".
<html>
<head>
<script type="text/javascript" src="delete_This.js"></script>
</head>
<body>
<h1>My First Web Page</h1>
<p id="demo"></p>
<input type="button" onclick="show()" value="Display Confirm">
</body>
</html>
这是javascript文件"delete_This.js".
function show()
{
var port = "8080";
var address_0="ws://192.168.1.2:" + port;
var address_1="ws://127.0.0.1:" + port;
var address_2="ws://localhost:" + port;
var ws;
if ("WebSocket" in window)
ws = new WebSocket(address_2); //843
else if("MozWebSocket" in window)
ws = new MozWebSocket(address_2); //843
else
alert("WebSockets NOT supported here!rnrnBrowser: " + navigator.appName + " " + navigator.appVersion + "rnrntest by jimbergman.net (based on Google sample code)");
try
{
ws.onmessage = function (evt)
{
var received_msg = evt.data;
};
ws.onopen = function(evt)
{
alert("connection opened");
// Web Socket is connected. You can send data by send() method
ws.send("message to send");
};
ws.onclose = function()
{
// websocket is closed. };
alert("WebSockets supported here!rnrnBrowser: " + navigator.appName + " " + navigator.appVersion + "rnrntest by jimbergman.net (based on Google sample code)");
}
}
catch(evt)
{
alert(evt.data);
}
}
这是服务器端的perl脚本"socketpolicy.pl".
use Socket;
use IO::Handle;
STDOUT->autoflush(1);
my $should_be_logging = 1; # change to 0 to turn off logging.
my $logfile = 'log';
if ($should_be_logging)
{
open(LOG, ">$logfile") or warn "Can't open $logfile: $!n";
LOG->autoflush(1);
}
my $port = 8080; #843
my $proto = getprotobyname('tcp');
# start the server:
#&log("Starting server on port $port");
print "Starting server on port $portn";
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "setsockopt: $!";
bind(Server,sockaddr_in($port,INADDR_ANY)) or die "bind: $!";
print "binding happenedn";
listen(Server,SOMAXCONN) or die "listen: $!";
Server->autoflush( 1 );
my $paddr;
#&log("Server started. Waiting for connections.");
print "Server started. Waiting for connections.n";
$/ = " "; # reset terminator to null char
# listening loop.
for ( ; $paddr = accept(Client,Server); close Client)
{
print "one connection acceptedn";
Client->autoflush(1);
my($port,$iaddr) = sockaddr_in($paddr);
my $ip_address = inet_ntoa($iaddr);
my $name = gethostbyaddr($iaddr,AF_INET) || $ip_address;
#&log( scalar localtime() . ": Connection from $name" );
print ( scalar localtime() . ": Connection from $namen" );
#print STDOUT <Client>;
#print Client "Welcome";
my $line = <Client>;
#&log("Input: $line");
print STDOUT $line;
my $client_Sent_Key;
if ($line =~ /("Sec-WebSocket-Key:")(.*)/i)
{
$client_Sent_Key = $2;
print STDOUT $client_Sent_Key;
}
print Client "Hello World";
#if ($line =~ /.*policy-file.*/i)
#{
# print Client &xml_policy;
#}
}
sub xml_policy
{
my $str = qq(<cross-domain-policy><allow-access-from domain="*" to-ports="*" /> </cross-domain-policy>