构建后不允许使用Angular 8方法的Socket io

时间:2020-08-10 18:38:09

标签: node.js socket.io angular8

我正在使用角度为8的套接字io更新记录异步。但这给了我以下错误。

开机自检 http://199.43.***.120:7171 / socket.io /?EIO = 3&transport = polling&t = NFPcfSj 405 (不允许使用方法)polyfills-es2015.19aa098ef024439a4e26.js:1

这是我用来构建服务器的代码。我已将app.js文件保存在src文件夹之外。

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

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

  socket: any;
  readonly uri: string = 'ws://199.43.***.120:7171/';
  constructor() {
    this.socket = io(this.uri);
    // this.socket = io(this.uri, { transport : ['websocket'] });
   }

  listen(eventName: string) {
    return new Observable((subscriber) => {
      this.socket.on(eventName, (data) => {
          subscriber.next(data);
      });
    });
  }

  emit(eventName: string, data: any) {
    this.socket.emit(eventName, data);
  }
}

这是我的app.js代码。

let app = require("express")();
let http = require("http").Server(app);
let io = require("socket.io")(http);

io.on("connection", socket => {
  console.log("user connected");
  

  socket.on('load appointment',function(id){
          io.emit('get user appointment',id);
  });

  socket.on('add appointment',function(id){
    io.emit('get user notification',id);
});


});

http.listen(5000, () => {
  console.log("started on port 5000");
});

这是我的代码调用事件监听器。

eventListenerForAppointments() {
                this.sockerService.listen('get user notification').subscribe((Id) => {
                  this.memberId = +Id;
                  this.getNotificationList();
                });
              }

注意:我已将app.js保留在src文件夹之外。我已经部署了我的角度应用程序远程IIS服务器。与ng:一起使用localhost:4000可以正常工作。但是部署后,这会在控制台上给我错误。

0 个答案:

没有答案
相关问题