我们如何使用socket.io有棱有角。?

问题描述 投票:-3回答:3

这是我的代码。我们如何使用socket.io有棱有角。?

import { Component } from '@angular/core';

import { createServer, Server } from 'http';

import * as express from 'express';

import * as socketIo from 'socket.io';





const SERVER_URL = 'localhost:4200';

@Component({




  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css']

})

export class AppComponent {


  public static readonly PORT:number = 8080;

  private app: express.Application;

  private server: Server;

  **"private io: SocketIO.server;"**  ////at this line i get error that cannot find namespace SocketIO

  private port: string | number;


}

我想在我的角度应用程序使用socket.io。而我得到的错误在大胆line.can任何一个可以帮助我,为什么我收到这个错误?

angular socket.io
3个回答
1
投票

这isn't使用Socket.io的最佳方式,我建议你使用这个模块

https://github.com/bougarfaoui/ng-socket-io

在项目中安装

npm install ng-socket-io

然后将其添加到您的应用程序模块

import { SocketIoModule, SocketIoConfig } from 'ng-socket-io';

const config: SocketIoConfig = { url: 'http://localhost:8988', options: {} 
};

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    SocketIoModule.forRoot(config) 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后,你就可以从注射扩展服务来使用它。


1
投票

https://www.npmjs.com/package/ngx-socket-io

npm install ngx-socket-io

比NG-插座-10,因为将有rxjs-COMPAT没有必要更好。


0
投票

NGX插槽-io为我所用 - 对角6的伟大工程

© www.soinside.com 2019 - 2024. All rights reserved.