【问题标题】:Socket connection is not established on AWS EC2AWS EC2 上未建立套接字连接
【发布时间】:2019-09-09 18:18:23
【问题描述】:

我已经使用 docker 映像部署了我的代码,这里是套接字连接的代码。

我在本地系统中获得了成功的连接,但是当我在 EC2 上部署相同的代码时,它给了我以下错误:

我正在使用快递服务器。

服务器:

var serverIO = require('http').Server(app);
var io = require('socket.io')(serverIO);
io.on('connection', function(client) {
    console.log('Client connected...', client.id);
})

var server = serverIO.listen(config.app.port, function(){
    console.log('server running on port:', config.app.port)
})

客户:

<!doctype html>
<html lang="en">
    <head>

    </head>
    <body>
        <h1>Hello World!</h1>
        <div id="future"></div>
        <form id="form" id="chat_form">
            <input id="chat_input" type="text">
            <input type="submit" value="Send">
        </form>
         <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
    </body>
</html>

<script>
 // var socket = io.connect('http://127.0.0.1:8000'); //here I got connection 
 var socket = io.connect('https://liveURL:8000'); //here I got error
    socket.on('connect', function(data) {
        socket.emit('join', {email: "user1@example.com"});
    });

  socket.on('broad', function(data) {
        console.log(data)
         $('#future').append(data+ "<br/>");
   });

  socket.on("new_msg", function(data) {
    console.log("here")
    alert(data.msg);
   })

 $('form').submit(function(e){
     e.preventDefault();
     var message = $('#chat_input').val();
     socket.emit('messages', {email: "user1@example.com"});
 });
</script>

【问题讨论】:

  • 试试 socket.on('error' 也许你会得到消息
  • @joyBlanks 服务器端没有打印错误
  • 还有一件事 :) 你在代码中隐藏了 IP,但在控制台屏幕截图中打印了红色列表;)
  • @Adiii 顺便说一句,这是错误的 URL。
  • @Adiii 谢谢,伙计。我很感激。

标签: javascript node.js amazon-web-services amazon-ec2 socket.io


【解决方案1】:

您需要将连接secure 标志设置为true。

 var socket = io.connect('https://liveURL:8000',{secure: true}); //here I got error
    socket.on('connect', function(data) {
        socket.emit('join', {email: "user1@example.com"});
    });

您可以查看官方文档。

{
  headers: /* the headers sent as part of the handshake */,
  time: /* the date of creation (as string) */,
  address: /* the ip of the client */,
  xdomain: /* whether the connection is cross-domain */,
  secure: /* whether the connection is secure */,
  issued: /* the date of creation (as unix timestamp) */,
  url: /* the request URL string */,
  query: /* the query object */
}

或者,如果您在服务器端使用 express,那么您可以查看 herehere

更新:

我看到您的服务器端没有 SSL 配置,所以 https 将无法工作。

改用 http 试试

io.connect('http://liveserver:8000')

【讨论】:

  • 但是您没有使用 ssl 在服务器端启动服务器?
  • 尝试连接像io.connect('http://liveserver:8000')这样的实时服务器
  • 我目前没有 SSL 证书。所以我无法使用 ssl 启动服务器
  • io.connect('http://liveserver:8000') 给出与图像中相同的错误
  • 那么您是如何尝试将客户端与https 连接的?所以尝试使用 http 连接
猜你喜欢
  • 2021-12-21
  • 2020-10-19
  • 2017-02-07
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2021-12-24
  • 2020-10-20
  • 1970-01-01
相关资源
最近更新 更多