简体   繁体   English

Socket.io 在部署后不工作 - 抛出错误 - 405(不允许)

[英]Socket.io is not working after deployment - throwing an error - 405 (Not Allowed)

Building an application with Angular and Node.使用 Angular 和 Node.js 构建应用程序。 Integrating a chat application built with Socket.io.集成使用 Socket.io 构建的聊天应用程序。 Working fine when tested locally.在本地测试时工作正常。 Even after deploying the Node JS backend to the server machine (AWS EC2 Instance), Our local angular app can access the backend server.即使在将 Node JS 后端部署到服务器机器(AWS EC2 实例)之后,我们的本地 angular 应用程序也可以访问后端服务器。 But after deploying the angular application in EC2, the chat module is not working and throwing an error " POST https://mysitedotcom/socket.io/?EIO=3&transport=polling&t=MzNa0m- 405 (Not Allowed) " Please find the server side code below.但是在 EC2 中部署 angular 应用程序后,聊天模块不工作并抛出错误“POST https://mysitedotcom/socket.io/?EIO=3&transport=polling&t=MzNa0m- 405 (Not Allowed)” 请找到服务器下面的侧面代码。

require('./global_constants');

const Knex = require('knex');
const morgan = require('morgan');
const express = require('express');
const bodyParser = require('body-parser');
const promiseRouter = require('express-promise-router');
const rs = require('rocket-store');
const knexConfig = require('./knexfile');
const registerApi = require('./api');
const adminName = require('./config/config').admin.name;
const dbObj = require('./controllers/dbConn');
const cleaner = require('./controllers/textCleaner');
const fs = require('fs');
const rimraf = require('rimraf');
const socketIO = require('socket.io');
const db = dbObj.db;

const {
  Model
} = require('objection');
const cors = require('cors');

const knex = Knex(knexConfig[process.env.NODE_ENV || 'production']);

Model.knex(knex);

const router = promiseRouter();

const app = express()
  .use(bodyParser.json())
  .use(morgan('dev'))
  .use(cors(
    {
      credentials: true,
      origin: ['http://localhost:4200', 'http://localhost:4201']
    }
  ))

app.use('/api/', router).set('json spaces', 2);

app.use((req, res, next) => {
  res.setHeader('Content-Type', 'application/json');
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
})


registerApi(router);

app.use((err, req, res, next) => {
  if (err) {
    console.error(err);
    res.status(err.statusCode || err.status || 500).send(err || {});
  } else {
    next();
  }
});

const http = require('https').createServer(app);
const io = socketIO(http);
http.listen(3000, function () {
  console.log('listening on *:3000');
});

rs.options({
  data_storage_area: "./msg",
  data_format: rs._FORMAT_JSON,
});


io.on('connection', socket => {
  socket.on('new-user', async (room, userId) => {
    // some logic goes here
  })

  socket.on('new-message', async (room) => {
    // Some logic
  })

  socket.on('user-disconnect', async (room, userId) => {
    // Some logic
  })
})

Please find the Client side code below请在下面找到客户端代码

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ChatService {

  private socket;
  constructor() {
    this.socket = io();
  }

  public newUser(roomName, id) {
    this.socket.emit('new-user', id, roomName);
  }
  public userDisconnected(roomName,id){
    this.socket.emit('user-disconnect',roomName,id)
  }

  public sendMessage(roomName, message,fullname,) {
    this.socket.emit('new-message',roomName,message,fullname);
  }

  public getconnect = () =>{
    return Observable.create((observer) => {
      this.socket.on('user-connected', (message,prev,socketId,flag) => {
          observer.next({msg:message,all_prev_msg:prev,socketId:socketId});
      });
    });
  }
  public getDisconnected = () =>{
    return Observable.create((observer) => {
      this.socket.on('user-disconnected', (message) => {
          observer.next(message);
      });
    });
  }

  public getMessages = () => {
    return Observable.create((observer) => {
      this.socket.on('new-message', (room,message,userId) => {
          observer.next({text:message,sender:userId});
      });
    });
  }

}

Please let me know if anyone has any more questions.如果有人有更多问题,请告诉我。 Many thanks.非常感谢。

Looks like you have nginx in your ec2 server, if yes you need to have the config below看起来你的 ec2 服务器中有 nginx,如果是,你需要有下面的配置

location /static {
    try_files $uri =404;
}

location /socket.io {
        proxy_pass http://node;
        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;
        add_header  Front-End-Https   on;
}

error_page 405 @nodejs;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM