Firefox-WebSocket-未建立连接



我正试图通过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>);
    return $str;
}
sub log
{
    my($msg) = @_;
    if ($should_be_logging) 
   {
       print LOG $msg,"n";
   #print $msg,"n";
   }
}

有趣的是,服务器正在接受连接并显示它有一个连接。但是,客户端(Firefox浏览器)显示连接尚未建立。并且有消息从客户端发送到服务器端。这是服务器端的显示器。

C:Documents and SettingsprabhakaranDesktop>perl socketpolicy.pl
Starting server on port 8080
binding happened
Server started. Waiting for connections.
one connection accepted
Tue Mar  6 21:50:47 2012: Connection from localhost

这是FireBug 在客户端的调试信息

Firefox can't establish a connection to the server at ws://localhost:8080/.
ws = new MozWebSocket(address_2); //843             delete_This.js (line 44)

神秘之处在于这一部分。我在浏览器中关闭页面后。服务器正在接收来自客户端的一些消息。这是服务器端的显示器。新消息在"Connection from localhost"之后开始。

C:Documents and SettingsprabhakaranDesktop>perl socketpolicy.pl
Starting server on port 8080
binding happened
Server started. Waiting for connections.
one connection accepted
Tue Mar  6 21:50:47 2012: Connection from localhost
GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.
2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive, Upgrade
Sec-WebSocket-Version: 8
Sec-WebSocket-Origin: null
Sec-WebSocket-Extensions: deflate-stream
Sec-WebSocket-Key: riHRie5CtAA9Z7sHOobAMg==
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

我的问题是

1) 如何成功建立连接?

2) 如何让WebSocket即时发送消息?

如果有人回答这些问题,我将非常感谢他们。

注意:我使用的是Firefox 10.0.0.2、Windows平台和Strawberry Perl。

您的服务器正试图将WebSocket连接视为普通套接字连接。但是WebSocket连接有自己的协议。请参阅IETF 6455规范。

总结:

  • WebSocket连接有一个HTTP友好握手,服务器必须正确响应(在服务器正确响应之前,客户端不会将连接注册为打开)。除其他外,这种握手使在与Web服务器相同的端口上运行WebSocket服务器变得更加容易(并重用现有的防火墙配置)。握手还允许某种程度的CORS安全性
  • 每个WebSocket帧/消息都包括一个标头,其中包括有效负载长度和有效负载类型等。在浏览器到服务器帧(但不是服务器到浏览器帧)的情况下,还使用应用于有效载荷的4字节运行XOR掩码来屏蔽有效载荷。掩码数据被包括作为报头之后的前4个字节

更新

在您的特定代码中:

my $line = <Client>;

实际上是为文件I/O而设计的,并且它正在尝试读取整个文件。这意味着它在到达文件末尾之前不会返回。在套接字上下文中,EOF表示套接字何时关闭。我想你真的想要一些更原始的东西,比如标准的套接字recv调用。您需要重新录制,直到收到指示握手结束的"\r\n\r\n",然后需要将累积的内容拆分为单独的标题行进行处理。

相关内容

  • 没有找到相关文章

最新更新