Node.js socket.io服务器是否需要连接到http服务器?

Node.js socket.io服务器是否需要连接到http服务器?,node.js,websocket,Node.js,Websocket,我第一次学习socket.io。 我想启动一个简单的套接字服务器。 我在互联网上看到的每一个例子都是这样的: var http = require('http'); var fs = require('fs'); // Loading the index file . html displayed to the client var server = http.createServer(function(req, res) { fs.readFile('./index.html', '

我第一次学习socket.io。 我想启动一个简单的套接字服务器。 我在互联网上看到的每一个例子都是这样的:

var http = require('http');
var fs = require('fs');

// Loading the index file . html displayed to the client
var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
    });
});

// Loading socket.io
var io = require('socket.io').listen(server);

// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
    console.log('A client is connected!');
});


server.listen(8080);
问题 我的问题的核心很简单:在所有这样的示例中,我看到io连接到现有http服务器进行侦听

这是要求吗? 若有,原因为何?websocket协议是否通过http传输

用例 我的用例可能有点奇怪:我有一个vue开发服务器,它必须保持运行,因为它提供实时重新加载,等等。。它正在侦听端口8080,它由vue cli服务提供,因此我无法将其更改为附加某些内容

此外,我还有一个“手工制作”的实验性smtp服务器,它是在node.js中制作的,在端口25上运行

我想使用websocket允许smtp服务器“post”和Vue.js webapp“侦听”

最终目标:用一个简单的webgui创建一个独立的smtp捕获所有邮件的系统,这样通过我的smtp发送的每封邮件都会被“推”到我的webapp中,而不会被存储,也不会真正发送

鉴于此,我想创建一个“独立”socket.io服务器。如果它只是需要一个http服务器,我会在一个特定的端口上创建第二个,以允许http->websocket升级;然后,我的web应用程序将简单地与此辅助服务器上的套接字服务器通信


我更喜欢这种方式,因为使用pm2,我可以运行每个服务而不必接触其他服务

WebSocket协议是一个单独的协议,它不是建立在HTTP之上的。然而,HTTP协议本身有一种升级机制,即WebSocket握手,它将简单的HTTP连接转换为WebSocket连接。而且,由于浏览器使用它,因此不可能在没有HTTP的情况下从浏览器建立WebSocket连接

旁注:socket.io也使用其他协议。如果WebSocket协议不可用,则返回给它们


下面是一个明显的观点:没有必要将一台服务器连接到另一台服务器。事实上,这是一种反模式。您通常希望组件尽可能独立,以便对其中一个组件的更改不会影响另一个组件。此外:即使两者都通过HTTP运行,也没有理由这样做。我发现这些教程至少有误导性。

我添加了一个smtp服务器的概念证明,该服务器只捕获所有接收到的邮件,但不发送到真正的目标

此外,它还会在socket.io连接上发出事件

所有这些都不使用http服务器

SMTP+socket.js服务器 VUE.JS示例代码 如果发生新的情况,socket.io服务器会自动通知此组件。 在这种情况下,它可以记录所有事件,但也可以保存整个电子邮件原始数据

<template>
  <div class="hello">
    <h1>Le email che abbiamo ricevuto per te</h1>
    <pre 
      v-for="(message, index) in messages" 
      :key="index"
    >{{ 
      message
    }}</pre>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      messages: [],
    }
  },
  sockets: {
    connect() {
      console.log('socket connected')
    },
    smtpAuth(val) {
      console.log('SMTP AUTH', val);
    },
    smtpConnect(val) {
      console.log('SMTP CONNECT', val);
    },
    smtpMailFrom(val) {
      console.log('SMTP MAIL FROM', val);
    },
    smtpRcptTo(val) {
      console.log('SMTP RCPT TO', val);
    },
    smtpData(val) {
      console.log('SMYP DATA', val);
      this.messages.push(val);
    }
  },
};
</script>

以pm2 Run ecosystem.config.js的形式运行它,你能给我举一个gow上的例子,说明不要将套接字服务器连接到另一台服务器吗?我在谷歌上找不到它,一个也找不到。@realtebo看看文档:在这里,你可以找到几种方法来设置独立的socket.io服务器。
<template>
  <div class="hello">
    <h1>Le email che abbiamo ricevuto per te</h1>
    <pre 
      v-for="(message, index) in messages" 
      :key="index"
    >{{ 
      message
    }}</pre>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      messages: [],
    }
  },
  sockets: {
    connect() {
      console.log('socket connected')
    },
    smtpAuth(val) {
      console.log('SMTP AUTH', val);
    },
    smtpConnect(val) {
      console.log('SMTP CONNECT', val);
    },
    smtpMailFrom(val) {
      console.log('SMTP MAIL FROM', val);
    },
    smtpRcptTo(val) {
      console.log('SMTP RCPT TO', val);
    },
    smtpData(val) {
      console.log('SMYP DATA', val);
      this.messages.push(val);
    }
  },
};
</script>
module.exports = {
  apps: [
    {
      name: "WEB",
      script: "./node_modules/@vue/cli-service/bin/vue-cli-service.js",
      args: "serve"
    },
    {
      watch: true,
      name: "SMTP",
      script: "./server/index.js"
    }
  ]
};