我在Amazon Web Services上托管了一台服务器。我在我的网站上使用socket.io和nodejs。以下是代码: 客户端 -
function bindSocket(){
iosocket = io.connect('http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080');
iosocket.on('connect', function () {
alert('connected');
iosocket.on('message', function(message) {
//alert(message);
getNotificationData(message);
//document.getElementById("socket_div").innerHTML = message;
});
iosocket.on('disconnect', function() {
//alert('disconnected');
});
});
}
服务器端 -
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
//res.end(fs.readFileSync(__dirname + '/socket_test.html'));
res.end();
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
console.log('new connection');
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
我收到以下错误消息:
polling-xhr.js:261 GET http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080/socket.io/?EIO=3&transport=polling&t=LjIkBx8 net::ERR_CONNECTION_TIMED_OUT
我一直在digitalocean服务器上使用相同的代码(更改了IP)。但是,我迁移到AWS并且我无法使其正常运行。
任何帮助都将受到高度赞赏。
答案 0 :(得分:2)
我明白了。必须从AWS安全组启用端口本身。我添加了规则,一切正常。 希望这有助于其他人。