gpt4 book ai didi

Perl Mojolicious 和 Socket.IO 运行不佳

转载 作者:行者123 更新时间:2023-12-03 03:46:09 24 4
gpt4 key购买 nike

我试图尝试使用 Socket.IO 和 Perl 的 Mojolicious。虽然我能够使用 WebSockets 执行相同的操作,但我尝试使用 Socket.IO 执行相同的操作(由于我需要更广泛的浏览器支持),但效果不佳.

我正在使用 Morbo 服务器。代码是:

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/' => sub {
my $self = shift;

$self->render('index');
};

websocket '/echo' => sub {
my $self = shift;
state $counter = 1;

$self->res->headers->header('Access-Control-Allow-Origin' => "*");
Mojo::IOLoop->recurring(1.5 => sub {
my $loop = shift;
$self->send(sprintf("Websocket request: %d\n",$counter++));

});
};

app->start;

__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO test</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
</head>
<body>
<div id="dumper"></div>
<script>
var ws = new WebSocket('<%= url_for('echo')->to_abs %>');

ws.onmessage = function(e) {
var message = e.data;
console.log(message);
$("#dumper").text(message);
};
// var socket = io.connect('<%= url_for('echo')->to_abs %>');
// socket.on('data', function (data) {
// console.log(data);
// });
</script>


</body>
</html>

此代码部分运行良好(WebSocket),当我取消注释 Socket.IO 部分时,它在 io.connect(...) 上失败 出现以下错误( header 和响应):

请求:

GET /socket.io/1/?t=1385234823089 HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31
Accept: */*
Referer: http://127.0.0.1:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

回应:

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Type: text/html;charset=UTF-8
Date: Sat, 23 Nov 2013 19:27:03 GMT
Content-Length: 6163
Server: Mojolicious (Perl)

使用WebSocket时从客户端收到的请求是这样的:

GET ws://127.0.0.1:3000/echo HTTP/1.1
Pragma: no-cache
Origin: http://127.0.0.1:3000
Host: 127.0.0.1:3000
Sec-WebSocket-Key: zgob5gss5XTr9vzYwYNe+A==
Upgrade: websocket
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cache-Control: no-cache
Connection: Upgrade
Sec-WebSocket-Version: 13

似乎Socket.IO没有请求套接字升级?另外,Mojo 不知道这条路线:

/socket.io/1

我也尝试更换:

io.connect('<%= url_for('echo')->to_abs %>');

with io.connect('http://localhost:300/echo');

最佳答案

在我看来,来自 CDN 的文件正在向您的服务器请求本应来自 CDN 的文件。我的猜测是需要额外的配置。

关于Perl Mojolicious 和 Socket.IO 运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com