用ngx-socket-io打开套接字不起作用

时间:2019-12-12 08:37:49

标签: angular sockets ionic-framework

我正在使用ionic 4开发一个应用程序,必须连接到具有wifi模块的电子板,该模块会生成wifi接入点。 我需要连接到该设备。

这是

的摘录
  

app.module.ts

const config: SocketIoConfig = { url: "192.168.1.1:5555", options: {} };

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    IonicStorageModule.forRoot(),
    SocketIoModule.forRoot(config)
  ],
  providers: [
    StatusBar,
    SplashScreen,
    Camera,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})

然后,由于我已实例化了套接字,所以我尝试了以下方法:

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

import { Platform } from "@ionic/angular";
import { SplashScreen } from "@ionic-native/splash-screen/ngx";
import { StatusBar } from "@ionic-native/status-bar/ngx";
import { Socket } from "ngx-socket-io";

@Component({
  selector: "app-root",
  templateUrl: "app.component.html"
})
export class AppComponent {
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private socket: Socket
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      // this.socket.on('connect', function() {
      //   console.log('connected');
      //   console.log('is connected? ', this.socket.connected);
      // });

      // this.socket.connect();

      console.log(this.socket);

      console.log("is connected? " + this.socket.ioSocket.connected);
    });
  }
}

我得到的连接是假的,并且对象是:

    WrappedSocket {config: {…}, subscribersCounter: 0, emptyConfig: {…}, ioSocket: Socket}
config: {url: "192.168.1.1:5555", options: {…}}
emptyConfig: {url: "", options: {…}}
ioSocket: Socket
acks: {}
connected: false
disconnected: true
flags: {}
ids: 0
io: Manager
autoConnect: true
backoff: Backoff {ms: 1000, max: 5000, factor: 2, jitter: 0.5, attempts: 0}
connecting: Array(1)
0: Socket
acks: {}
connected: false
disconnected: true
flags: {}
ids: 0
io: Manager {nsps: {…}, subs: Array(3), opts: {…}, _reconnection: true, _reconnectionAttempts: Infinity, …}
json: Socket {io: Manager, nsp: "/", json: Socket, ids: 0, acks: {…}, …}
nsp: "/"
receiveBuffer: []
sendBuffer: []
subs: (3) [{…}, {…}, {…}]
_callbacks: {$connecting: Array(1), $connect: Array(1)}
__proto__: Object
length: 1
__proto__: Array(0)
decoder: Decoder {reconstructor: null}
encoder: Encoder {}
encoding: false
engine: Socket {secure: false, agent: false, hostname: "192.168.1.1", port: "5555", query: {…}, …}
lastPing: null
nsps: {/: Socket}
opts: {path: "/socket.io", hostname: "192.168.1.1", secure: false, port: "5555"}
packetBuffer: []
readyState: "opening"
skipReconnect: false
subs: (3) [{…}, {…}, {…}]
uri: "http://192.168.1.1:5555"
_callbacks: {$open: Array(1), $packet: Array(1), $close: Array(1)}
_randomizationFactor: 0.5
_reconnection: true
_reconnectionAttempts: Infinity
_reconnectionDelay: 1000
_reconnectionDelayMax: 5000
_timeout: 20000
__proto__: Object
json: Socket {io: Manager, nsp: "/", json: Socket, ids: 0, acks: {…}, …}
nsp: "/"
receiveBuffer: []
sendBuffer: []
subs: (3) [{…}, {…}, {…}]
_callbacks: {$connecting: Array(1), $connect: Array(1)}
__proto__: Object
subscribersCounter: 0

enter image description here

任何帮助将不胜感激。谢谢

0 个答案:

没有答案