使用Socket.io与两个不同的localhost端口进行交互?

时间:2017-08-01 03:45:46

标签: javascript ios node.js sockets

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

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>

然后我的另一个文件(我称之为pi.js),我在端口3001上托管,我希望能够使用它与端口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连接,那么它们应该能够进行通信。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我的选择:因为您缺乏将2个彼此连接的中间端。我建议您尝试使用redis。在您的情况下,redis介于两者之间(例如服务器),当clinet1在redis上发送消息(subcrise)时,redis将查找cli1的消息并将其响应给client2({{ 3}})。但是,当用户离线时,记忆体就不属于人们了