我已经用docker image部署了代码,这是套接字连接的代码。
我在本地系统中获得成功连接,但是当我在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>
答案 0 :(得分: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,则可以查看here和here
已更新:
我发现您的服务器端没有 SSL配置,因此https
无法正常工作。
尝试使用http代替
io.connect('http://liveserver:8000')