简体   繁体   English

在现有页面上使用Socket.io和Node.js(使用Nginx)吗?

[英]Use Socket.io and Node.js on existing page (with nginx)?

I'm trying to expand an existing Webpage (NGINX + PHP5 + MySQL) with a real time API that feeds some additional data (for example, an in-page chat). 我正在尝试使用实时API扩展现有网页(NGINX + PHP5 + MySQL),该API会提供一些其他数据(例如,页内聊天)。

After going trough some standalone testscripts, etc, I'm actually already having troubles just to set up a simple chat example: Socket.io doesnt want to work, apparently because of dependencies. 经过一些独立的测试脚本等之后,实际上我已经遇到了麻烦,只是为了建立一个简单的聊天示例:Socket.io不想工作,显然是因为依赖关系。

I'm trying to use NodeJS/Socket.io only as alternative to fdajax - I never intended to build my whole (already existing) page on nodejs. 我试图使用NodeJS / Socket.io来替代fdajax-我从没打算在nodejs上构建我的整个(已经存在)页面。 So, my main question is, how can I 'implement' those two libraries, and still make them work (even if socket.io.js isnt delivered by nodejs ) 所以,我的主要问题是,如何“实现”这两个库,并使它们仍然有效(即使socket.io.js不是由nodejs交付的)

My current error is ReferenceError: require is not defined and ReferenceError: io is not defined when including socket.io.js . 我当前的错误是在包含socket.io.jsReferenceError: require is not defined ReferenceError: io is not defined Apparently, the reason is that the function require() probably doesnt work properly, since - well, nodejs isnt delivering the whole page, but PHP does (so, I guess, the whole require() stuff is missing). 显然,原因是require()函数可能无法正常工作,因为-不错,nodejs不能交付整个页面,而PHP可以(因此,我想缺少了全部require()的东西)。

Does someone know how I can get socket.io to work, whitout having nodejs deliver the whole page, but instead still using the already existing NGINX+PHP setup? 有人知道我如何才能使socket.io正常工作,而没有让nodejs交付整个页面,而是仍然使用现有的NGINX + PHP设置吗?

I tried a couple of things, but couldnt find a good hint yet. 我尝试了几件事,但找不到很好的提示。 The target basically is, to just include socket.io.js (and other required stuff), connect to the nodejs webserver over the specified port (8080, or whatever), and then write some code. 基本上,目标是仅包含socket.io.js (和其他必需的东西),通过指定的端口(8080或任何其他端口)连接到nodejs Web服务器,然后编写一些代码。

Thanks for the help. 谢谢您的帮助。

Here is a full fledged example from another answer/question https://stackoverflow.com/a/14709236/1489655 这是另一个答案/问题的完整示例 https://stackoverflow.com/a/14709236/1489655


You can setup a proxy for the websocket traffic. 您可以为websocket流量设置代理。

tcp {
    upstream websockets {
        ## node processes
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
        server 127.0.0.1:8004; 

        check interval=3000 rise=2 fall=5 timeout=1000;
    }   

    server {
        listen 127.0.0.1:80;
        server_name _;

        tcp_nodelay on;
        proxy_pass websockets;
    }
}

Source: http://www.letseehere.com/reverse-proxy-web-sockets 来源: http//www.letseehere.com/reverse-proxy-web-sockets

This should allow you to connect to socket.io via 这应该允许您通过以下方式连接到socket.io

var socket = new io.Socket('ws://localhost');

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

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