简体   繁体   English

Websocket和Perl CGI

[英]Websocket and Perl CGI

I am bit new in CGI programming, and I trying to make an online chat API but face not few troubles: 我对CGI编程有点陌生,我尝试制作一个在线聊天API,但遇到了不少麻烦:

I was looking online for solution and found Websocket for client (js) and HTTP::Daemon for perl, but I have no idea where to start to make the server listen for the connections from the browser. 我在网上寻找解决方案,发现Websocket用于客户端(js),而HTTP :: Daemon用于perl,但是我不知道从哪里开始让服务器侦听来自浏览器的连接。

Here is my JavaScript code: 这是我的JavaScript代码:

ws = new WebSocket('ws://www.crazygao.com:3000'); // test
ws.onopen = function() {
    alert('Connection is established!'); // test
};
ws.onclose = function() {
    alert('Connection is closed'); 
};
ws.onmessage = function(e) {
    var message = e.data;
    //alert('Got new message: ' + message);
};
ws.onerror = function(e) {
    //var message = e.data;
    alert('Error: ' + e);
};

Here is my Perl script test code: 这是我的Perl脚本测试代码:

use HTTP::Daemon;
use HTTP::Status;

my $d = HTTP::Daemon->new(
    LocalAddr => 'www.crazygao.com',
    LocalPort => 3000
) || die; print "Please contact me at: <URL:", $d->url, ">\n";
while(my $c = $d->accept) {
    $c->send_response("1"); # test
    while (my $r = $c->get_request) {
        if ($r->method eq 'GET') {
            $c->send_response("...");
        }
    }
    $c->close;
    undef($c);
}

When the page loads, the connection closing immediately, and in Chrome console window I see the following error: WebSocket connection to 'ws://198.38.89.14:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED 页面加载后,连接立即关闭,并且在Chrome控制台窗口中看到以下错误:与'ws://198.38.89.14:3000 /'的WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_REFUSED

I run the perl script manually (using simple call to http://example.com/cgi-bin/xxx.cgi ) and then when I refresh the page I get: WebSocket connection to 'ws://198.38.89.14:3000/' failed: Error during WebSocket handshake: Unexpected response code: 200 我手动运行perl脚本(使用对http://example.com/cgi-bin/xxx.cgi的简单调用),然后刷新页面时得到:WebSocket连接到'ws://198.38.89.14:3000 /'失败:WebSocket握手期间出错:意外的响应代码:200

I understand that the server normally returns 200 when OK, but Websocket is waiting for 101 code as "OK". 我知道服务器正常时返回正常200,但是Websocket正在等待101代码为“确定”。

My question is, if so, how can I achieve this? 我的问题是,如果是这样,我该如何实现?

I know this is old and I got here because I was looking for an answer myself. 我知道这很老,我来到这里是因为我自己正在寻找答案。 I ended up finding the answer myself by using Net::WebSocket::Server. 我最终通过使用Net :: WebSocket :: Server自己找到了答案。

http://search.cpan.org/~topaz/Net-WebSocket-Server-0.003004/lib/Net/WebSocket/Server.pm for more details on how to use the module and example. http://search.cpan.org/~topaz/Net-WebSocket-Server-0.003004/lib/Net/WebSocket/Server.pm了解有关如何使用该模块和示例的更多详细信息。

Basically, you'll have this perl code to match your javascript (copied and modified from the CPAN page of Net::WebSocket::Server): 基本上,您将具有以下perl代码以匹配您的javascript(从Net :: WebSocket :: Server的CPAN页面复制和修改):

use Net::WebSocket::Server;

my $origin = 'http://www.crazygao.com';

Net::WebSocket::Server->new(
    listen => 3000,
    on_connect => sub {
        my ($serv, $conn) = @_;
        $conn->on(
            handshake => sub {
                my ($conn, $handshake) = @_;
                $conn->disconnect() unless $handshake->req->origin eq $origin;
            },
            utf8 => sub {
                my ($conn, $msg) = @_;
                $_->send_utf8($msg) for $conn->server->connections;
            },
            binary => sub {
                my ($conn, $msg) = @_;
                $_->send_binary($msg) for $conn->server->connections;
            },
        );
    },
)->start;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM