阿帕奇+ Symfony2的+ HTTPS + Node.js的+ Socket.io:socket.emit不点火 [英] Apache + Symfony2 + HTTPS + Node.js + Socket.io: socket.emit not firing

查看:124
本文介绍了阿帕奇+ Symfony2的+ HTTPS + Node.js的+ Socket.io:socket.emit不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上对小时花费数小时,但无济于事。

I've been spending hours upon hours on this problem, but to no avail.

我在建的Symfony2 的一个项目,这需要上传大文件的模块。我选择了 Node.js的 Socket.IO (我不得不从头学习它,所以我可能缺少基本的东西)。
我与HTML5文件和API的FileReader的结合这将文件从客户端发送的切片到服务器。
preliminary试验表明这种方法工作的伟大作为一个独立的应用程序,在那里一切都被处理,并送达 Node.js的,但集成的Apache 的Symfony2 似乎有问题。

I'm building a project in Symfony2, which requires a module for uploading large files. I've opted for Node.js and Socket.IO (I had to learn it from scratch, so I might be missing something basic). I'm combining these with the HTML5 File and FileReader API's to send the file in slices from the client to the server. Preliminary tests showed this approach working great as a standalone app, where everything was handled and served by Node.js, but integration with Apache and Symfony2 seems problematic.

该应用程序有一个安全和安全部分。我的目标是使用的Apache 上的端口 80 443 为服务于大宗建应用程序的Symfony2 Node.js的插座。 IO 端口 8080 文件上传。连接到插座的客户端页面将的Apache 送达,但插座将通过 Node.js的运行。上传模块运行通过HTTPS,在页面驻留在同一个身份验证的用户一个安全的环境。

The application has an unsecured and secured section. My goal is to use Apache on ports 80 and 443 for serving the bulk of the app built in Symfony2, and Node.js with Socket.io on port 8080 for file uploads. The client-side page connecting to the socket will be served by Apache, but the socket will run via Node.js. The upload module has to run over HTTPS, as the page resides in a secured environment with an authenticated user.

问题是使用 socket.emit socket.send 好像不工作的事件。客户端到服务器或服务器到客户端,它使没有什么区别。的什么也没有发生的,有没有错误。

The problem is events using socket.emit or socket.send don't seem to work. Client to server, or server to client, it makes no difference. Nothing happens and there are no errors.

中显示的code是我的code的简化版本,没有杂波和敏感数据。

The code shown is a simplified version of my code, without the clutter and sensitive data.

var httpsModule = require('https'),
    fs = require('fs'),
    io = require('socket.io');

var httpsOptions = 
{
    key: fs.readFileSync('path/to/key'),
    cert: fs.readFileSync('/path/to/cert'),
    passphrase: "1234lol"
}

var httpsServer = httpsModule.createServer(httpsOptions);
var ioServer = io.listen(httpsServer);

httpsServer.listen(8080);

ioServer.sockets.on('connection', function(socket)
{
    // This event gets bound, but never fires
    socket.on('NewFile', function(data)
    {
        // To make sure something is happening
        console.log(data);

        // Process the new file...
    });

    // Oddly, this one does fire
    socket.on('disconnect', function()
    {
        console.log("Disconnected");
    });
});

客户端

// This is a Twig template, so I'll give an excerpt
{% block javascripts %}
{{ parent() }}

<script type="text/javascript" src="https://my.server:8080/socket.io/socket.io.js"></script>
<script type="text/javascript">

    var socket = io.connect("my.server:8080",
                {
                    secure: true,
                    port: 8080
                });


    // Imagine this is the function initiating the file upload
    // File is an object containing metadata about the file like, filename, size, etc.
    function uploadNewFile(file)
    {
         socket.emit("NewFile", item);
    }

</script>
{% endblock %}

所以,问题是...

当然还有更多比这个应用程序,但是这就是我卡住了。该页面加载完全没有错误,但发出事件永远不会触发或达到服务器(除断开事件)。我已经与客户端和服务器上的消息时试图检查它是否是只用自定义事件的问题,但是这也不能工作。我猜的东西阻止客户端服务器通信(它不是防火墙,我已经检查)。

So the problem is...

Of course there's much more to the application than this, but this is where I'm stuck. The page loads perfectly without errors, but the emit events never fire or reach the server (except for the disconnect event). I've tried with the message event on both client and server to check if it was a problem with only custom events, but that didn't work either. I'm guessing something is blocking client-server communication (it isn't the firewall, I've checked).

我完全是在这里的损失,所以请大家帮忙。

I'm completely at a loss here, so please help.

推荐答案

一些艰苦的调试后,我发现了什么是错的我的设置。还不如分享我的研究结果,虽然他们是(我认为)无关的Node.js,Socket.IO或Apache。

After some painstaking debugging, I've found what was wrong with my setup. Might as well share my findings, although they are (I think) unrelated to Node.js, Socket.IO or Apache.

正如我所说,我的问题就简化code向你展示我的设置,但不杂乱。我是,但是,设置客户端通过一个对象,使用属性来配置套接字连接。像这样:

As I mentioned, my question had simplified code to show you my setup without clutter. I was, however, setting up the client through an object, using the properties to configure the socket connection. Like so:

var MyProject = {};

MyProject.Uploader =
{
    location: 'my.server:8080',
    socket: io.connect(location,
            {
                secure: true,
                port: 8080,
                query: "token=blabla"
            }),
    // ...lots of extra properties and methods
}

问题在使用位置作为属性名称所在。它是在Javascript保留字,使得在这种情况下,一些奇怪的行为。我觉得很奇怪,一个对象的属性名称不能是保留字,所以我决定测试。我也注意到我被错误地引用了财产,我忘了用 this.location 当连接到插座。所以我改成了这一点,只是作为一个测试。

The problem lay in the use of location as a property name. It is a reserved word in Javascript and makes for some strange behaviour in this case. I found it strange that an object's property name can't be a reserved word, so I decided to test. I had also noticed I was referencing the property incorrectly, I forgot to use this.location when connection to the socket. So I changed it to this, just as a test.

var MyProject = {};

MyProject.Uploader =
{
    location: 'my.server:8080',
    socket: io.connect(this.location,
            {
                secure: true,
                port: 8080,
                query: "token=blabla"
            }),
    // ...lots of extra properties and methods
}

但无济于事。我仍然没有结束套接字获取数据。因此,下一步似乎我的无奈驱动调试愤怒的逻辑。改变了属性名固定的一切!

But to no avail. I was still not getting data over the socket. So the next step seemed logical in my frustration-driven debugging rage. Changing up the property name fixed everything!

var MyProject = {};

MyProject.Uploader =
{
    socketLocation: 'my.server:8080',
    socket: io.connect(this.socketLocation,
            {
                secure: true,
                port: 8080,
                query: "token=blabla"
            }),
    // ...lots of extra properties and methods
}

这个方法非常完美,我正在调试信息的负荷。成功!!
无论是预期的行为在Javascript重写(或任何发生在这里,误用感觉就像把它给我现在的一个更好的办法)对象的属性,如果你碰巧使用保留字,我不知道。我只知道我明确转向他们从现在开始!

This approach worked perfectly, I was getting loads of debug messages. SUCCESS!! Whether it is expected behaviour in Javascript to override (or whatever is happening here, "to misuse" feels like a better way of putting it to me right now) object properties if you happen to use a reserved word, I don't know. I only know I'm steering clear of them from now on!

希望它可以帮助那里的人!

Hope it helps anyone out there!

这篇关于阿帕奇+ Symfony2的+ HTTPS + Node.js的+ Socket.io:socket.emit不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆