【问题标题】:Making xampp and node(socket.io) work together使 xampp 和 node(socket.io) 一起工作
【发布时间】:2016-06-11 13:00:20
【问题描述】:

我有一个小网络应用程序,它为一些函数和来自数据库的一些查询(xampp 的 Apache 和 mySQL)应用了一些 AngularJS 和 php。现在我想申请一个简单的网络应用程序聊天,我不知道如何配置也不知道什么端口等等。

关注类似于http://socket.io/get-started/chat/的内容

我有 index.js 这样的

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
   res.sendFile(__dirname + '/index.html');

});

io.on('connection', function(socket){
   console.log('a user connected');
   socket.on('disconnect', function(){
   console.log('user disconnected');
   });
});


http.listen(443, function(){
  console.log('listening on *:443');
});

我正在尝试端口 443 和 80,但它们似乎不起作用(xampp 显示 80 和 443,不知道端口如何工作)

然后我的 JS 文件有 var socket = io();

在我的浏览器控制台中,我得到了类似

socket.io-1.2.0.js:2 GET http://localhost/socket.io/?IO=3&transport=polling&t=1465649664392-0 404 (Not Found)

我该如何解决这个问题?运行node index.js 后,我期望在cmd 中输出诸如a user connected 之类的输出,但我没有。

我按照聊天的例子,没有xampp也能正常工作。

【问题讨论】:

    标签: node.js sockets socket.io


    【解决方案1】:

    您的问题是,socket.io 需要能够通过 http 与后端通信(例如与switch the protocol)。但是如果 xampp 正在使用该端口,它将无法工作。您可能可以尝试让您的 xampp 将请求转发到 http://localhost/socket.io/*node.js 服务器。

    您可以使用ProxyPass directive in the Apache httpd.conf 并执行类似的操作

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:8000/$1 [P,L]
    
    ProxyPass        /socket.io http://localhost:8000/socket.io
    ProxyPassReverse /socket.io http://localhost:8000/socket.io
    

    还要确保所需的模块已启用

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
    

    然后您必须在端口8000 上启动node.js 服务器

    http.listen(8000, function(){
      console.log('listening on *:8000');
    }); 
    

    【讨论】:

    • 我只是将指令放在配置文件的任何位置吗?我是否可以正常访问xampp,例如localhost/foldername
    • 一切正常。您可以在httpd.conf 的任何位置添加指令。我在文件末尾添加了它。
    • 可以举个例子吗?我做了你说的一切,没有任何改变(我可能缺乏这些东西的知识)
    • 我的apache/conf/httpd.confpastebin.com/ruihe9GR。由于其中的绝对路径,您可能无法复制整个文件。我的main.js 文件是这个pastebin.com/CTfgNJmS。我用node main.js 运行它。我的index.php 文件是pastebin.com/YWh7haPF。我在htdocs/php-test/index.php 中有这个文件。我在浏览器中的http://localhost/php-test/ 下访问该示例。我在windows 10下使用的是windows版本的xampp。哦,更改httpd.conf后需要重启apache@
    【解决方案2】:

    您可以简单地执行此操作,无需任何 http 重定向 打开你的客户端js

    var configUrl = 'http://localhost:3000';// currently my node js using this port
    var socket = io(configUrl); 
    

    剩下的事情在您的客户端运行顺利

    socket.emit('chatMessage', ...
    socket.on('chatMessage', ..
    

    【讨论】:

      【解决方案3】:

      我们可以使用 scoket.io 客户端库,所以我们可以使用 JavaScript 作为客户端,使用 node.js 作为服务器。

      请查看以下示例:

      index.html(客户端)

      <!DOCTYPE html>
      <html>
      <head>
        <title>Hello world</title>
        <script src="https://cdn.socket.io/4.0.1/socket.io.js"></script>
      </head>
      <body>
        <script>
          var socket = io('http://localhost:5000');
        </script>
      </body>
      </html>
      

      app.js(Node.js 服务器)

      var io=require('socket.io')(5000,{
        cors:{
          origin: "http://localhost",
        },
      });
      
      
      //Whenever someone connects this gets executed
      io.on('connection',socket => {
       console.log('A user connected');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        相关资源
        最近更新 更多