socket.io 未在 aws 服务器中连接 [英] socket.io not connecting in aws server
问题描述
我有 node js express 项目.我正在为聊天应用程序实现 socket.io.要检查套接字连接,我正在使用此工具.https://amritb.github.io/socketio-client-tool/
i have node js express project. I m implementing socket.io for chat applications. To check Socket connection, i m using this tool. https://amritb.github.io/socketio-client-tool/
这是我的服务器代码.
const express = require('express');
const http = require('http');
expressValidator = require('express-validator');
const morgan = require('morgan');
const bodyParser = require("body-parser");
const compression = require('compression');
const logger = require('./components/helper/logger');
const multer = require('multer');
// const uuidV4 = require('uuid/v4');
const moment = require("moment");
const fs = require('fs')
const cors = require("cors");
const clientAuthentication = require('./middleware/clientAuthentication');
const config = require('./config/config');
const app = express();
const jsonParser = bodyParser.json();
const port = config.host.port || "3001";
app.set("port", port);
const server = http.createServer(app);
app.use(cors());
//////////////////////socket/////////
var io = require("socket.io")(server,{
cors: {
origin: '*',
},
});
// var socketmodule = require('./components/chat/socketmodule')(io)
//added temp
const {joinUser, removeUser, findUser} = require('./components/chat/users');
let thisRoom = "";
io.on("connection", function (socket) {
console.log("connected");
socket.on("join room", (data) => {
console.log('in room');
let Newuser = joinUser(socket.id, data.username, data.roomName)
socket.emit('send data', { id: socket.id, username: Newuser.username, roomname: Newuser.roomname });
thisRoom = Newuser.roomname;
console.log(Newuser);
socket.join(Newuser.roomname);
});
socket.on("chat message", (data) => {
io.to(thisRoom).emit("chat message", { data: data, id: socket.id });
});
socket.on("disconnect", () => {
const user = removeUser(socket.id);
console.log(user);
if (user) {
console.log(user.username + ' has left');
}
console.log("disconnected");
});
});
//added temp
app.get("/", function (req, res) {
res.sendFile(__dirname + "/chatTest.html");
});
////////////////////socket///////
server.listen(port);
logger.info(config.host.url);
logger.info("API server started on: " + port);
我使用上述工具检查了连接.像 http://localhost:3000,和套接字连接.但是当我在服务器中检查它时,它不起作用.
I checked the connection using above tool. like http://localhost:3000, and socket connected. But when I check it in server, its not working.
我在服务器中使用 ngnix 服务器并映射我的端口,我正在做反向代理
I m using ngnix server in server and to map my port i m doing reverse proxy like
location /hiyup_dev/ {
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
我检查过http://example.com/hiyup_dev(它将指向端口 3001)它不工作.
I checked like http://example.com/hiyup_dev (it will point to port 3001) its not working.
然后我在入站 aws 规则中添加了我的端口.然后我检查了一下http://myip:3001 它也不起作用.
Then i added my port in inbound aws rules. Then i checked like http://myip:3001 it also not working.
我在这里缺少什么.它在本地而不是服务器中工作正常.:(
What i am missing here. It works fine in local not in server. :(
推荐答案
客户端(swift)集成的问题.它仅支持套接字版本 2+/3+.所以我将服务器套接字版本从 4 降级到 3.然后我就成功了.
The Problem with the Client-side(swift) integration. It supports only socket version2+/3+. So i downgraded server socket version from 4 to 3. and i got it worked.
这篇关于socket.io 未在 aws 服务器中连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!