【问题标题】:Using Socket.io to interact with two different localhost ports?使用 Socket.io 与两个不同的本地主机端口进行交互?
【发布时间】:2018-01-07 18:39:35
【问题描述】:

这是我第一次使用 Socket.io。我有一个在两个不同文件上创建的聊天应用程序; server.js 和 pi.js,每个都托管在两个不同的端口上(3000 上的服务器,3001 上的 pi)。我指的是https://socket.io/get-started/chat/ 作为指导。见下文(注意这个文件中有很多模块;我已经构建了一个用于单独目的的网络应用程序,因此在这种情况下可以忽略其中的大部分):

server.js

const express = require('express'),
  app = express(),
  port = process.env.PORT || 3000,
  mongoose = require('mongoose'),
  pug = require('pug'),
  router = express.Router(),
  User = require('./api/models/userAPIModel'),
  bodyParser = require('body-parser'),
  path = require('path'),
  cool = require('cool-ascii-faces'),
  pg = require('pg'),
  http = require('http').Server(app),
  io = require('socket.io')(http),
  request = require('request');

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

var clients = 0;

io.on('connection', function(socket) { //on connection, it will show up in the console, and vice versa.
  clients++;
  console.log('User connected. Clients currently connected: ' + clients);

  socket.on('chat message', function(msg) {
    console.log('message: ' + msg);
    io.sockets.emit('chat message', msg); 
  });

  socket.on('disconnect', function() {
    clients--;
    console.log('user disconnected');
  });
});

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

我的 HTML 文件:

<!doctype html>
<html>

    <head>
        <title>Socket</title>
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body { font: 13px Helvetica, Arial; }
            form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
            form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
            form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
            #messages { list-style-type: none; margin: 0; padding: 0; }
            #messages li { padding: 5px 10px; }
            #messages li:nth-child(odd) { background: #eee; }
        </style>    
    </head>

    <body>
        <ul id="messages"></ul>
        <form action="">
            <input id="m" autocomplete="off" /><button>Send</button>
        </form>

        <script src="/socket.io/socket.io.js"></script>
        <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
        <script>var socket = io();</script>
        <script>
        $(function () {
            var socket = io();

            $('form').submit(function() {
            socket.emit('chat message', $('#m').val());
            $('#m').val('');
            return false;
            });

            socket.on('chat message', function(msg){
            $('#messages').append($('<li>').text(msg));
            });
        });
        </script>
    </body>

</html>

然后是我在端口 3001 上托管的另一个文件(我称之为 pi.js),我希望能够使用它与端口 3000 通信,但它不起作用:

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

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

// io.connect('http://localhost:3000');

    var clients = 0;

    io.on('connection', function(socket) { //on connection, it will show up in the console, and vice versa.
      clients++;
      console.log('User connected. Clients currently connected: ' + clients);

      socket.on('chat message', function(msg) {
        console.log('message: ' + msg);
        io.sockets.emit('chat message', msg); 
      });

      socket.on('disconnect', function() {
        clients--;
        console.log('user disconnected');
      });
    });


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

当我对此进行测试时,server.js 和 pi.js 没有按照我想要的方式进行通信,因为当我打开这两个应用程序时,我可以在其中一个应用程序上输入一条消息,但它不会显示另一方面。当我在两个不同的选项卡中打开其中一个文件时,它们能够相互通信。但是,我假设因为 HTML 文档具有与端口 3000 连接的脚本标记,所以它们应该能够通信。我在这里做错了什么?

【问题讨论】:

  • 为什么需要它是不同的端口?那么,您希望两个不同的站点相互通信吗?
  • @Alex Marvick 为什么要在两个不同的端口上运行它?使用两个不同的文件有什么意义?您可以使用同一文件处理多个客户端
  • @Hatjhie 这是我被分配去弄清楚的事情——我也不完全确定它的重要性,因为,Rohan,你是对的——我只需要一台服务器来处理多个客户端.我想我最终会尝试链接两个不同的网站
  • @mehta-rohan 参考以上评论
  • 请备份并在此处描述实际分配。 socket.io 允许客户端和服务器使用持久的 2 路通信通道进行通信。您可以让 N 个不同的客户端都与服务器建立自己的通信通道,但每个通信通道都在客户端和服务器之间。如果您希望这些客户端之一使用服务器作为网关然后连接到其他东西,您必须编写自己的胶水从客户端 A 获取消息,然后将其发送到其他连接。 socket.io 不会以任何方式为您这样做。

标签: javascript ios node.js sockets


【解决方案1】:

我的意见:因为您缺少将两个彼此连接的中间端。我建议你尝试使用redis。在你的情况下,redis on between(如服务器),当clinet1在redis上发送消息(subcrise)时,redis会找到cli1的消息并将其响应给client2(publish)。但是请记住当用户离线时取消订阅

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2021-08-20
    相关资源
    最近更新 更多