会说话的AWS服务器(EC2)

时间:2018-07-27 16:47:52

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

我正在尝试让两个EC2实例进行对话;但是,我遇到了一个障碍。我不确定要使用哪个IP地址和端口,并且我想知道既然它们不在同一台计算机上,我是否需要更改任何服务器/客户端代码。安全组允许端口22,80和60201上的入站流量。

使用端口60201时,我得到EADDRNOTAVAIL。

这是配置文件。

Host wmnode
    User ubuntu
    IdentityFile ~/.ssh/wmnode
    HostName 52.4.212.112
Host mvclient
    User ubuntu
    IdentityFile ~/.ssh/mvclient
    HostName 107.21.65.254

这是server.js

const port = 60201;
const ipAddress = "52.4.212.112"

var http = require('http');
var io = require('socket.io');
var server = http.createServer();
server.listen(port, ipAddress);
var socket = io.listen(server);

socket.on('connection', (serverSocket) => {
    console.log('connected to 52.4.212.112');
    serverSocket.on('clientEvent', function (data) {
        console.log('message from the server:', data);
        serverSocket.emit('serverEvent', "thanks for sending '" + data + "'");
    });
});

这是client.js

const port = 60201;
const ipAddress = "52.4.212.112";
const url = 'http://' + ipAddress + ':' + port;

var io = require('socket.io-client');

var socket = io(url);

socket.on('connect', () => {
    socket.on('serverEvent', function (data) {
        console.log('new message from the server:', data);
    });
    setInterval(function () {
        socket.emit('clientEvent', Math.random());
        console.log('message sent from the client');
    }, 3000);
});

Error in its entirity

0 个答案:

没有答案