Socket-io客户端未连接到Node js套接字io



我正在尝试在离子电容器(基于角度的应用程序(中实现socket-io客户端,但无法将客户端连接到服务器。

这是我在应用程序模块中使用的套接字服务。

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import {environment} from '../environments/environment'
@Injectable({
providedIn: 'root'


import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import {environment} from '../environments/environment'
@Injectable({
providedIn: 'root'
})
export class SocketioService {
socket:any;
constructor() { 
this.socket = io('http://localhost:5000',{ transports : ['websocket'] });
console.log("socket",this.socket)
}
setupSocketConnection() {
console.log("socketio from socket service")
this.socket = io(environment.SOCKET_ENDPOINT);
console.log("socket",this.socket)
this.socket.emit('my message', 'Hello there from Angular.');
this.socket.on("test",(arg)=>{
console.log(arg)
})
}
}

<!--开始片段:js-hide:false控制台:true-babel:false--><!--语言:lang-js->这是我在尝试控制台客户端上的套接字时得到的

socket from app 
p
acks: {0: ƒ}
connected: false
disconnected: true
flags: {}
ids: 1
io: p {nsps: {…}, subs: Array(3), opts: {…}, _reconnection: true, _reconnectionAttempts: Infinity, …}
json: p {io: p, nsp: "/", json: p, ids: 1, acks: {…}, …}
nsp: "/"
receiveBuffer: []
sendBuffer: [{…}]
subs: (3) [{…}, {…}, {…}]
_callbacks: {$connecting: Array(1), $connect: Array(1), $connection: Array(1)}
__proto__: Object

您应该为客户端使用ngx套接字io库,而不是使用socket.io-client库。

app.module.ts

类似导入:

import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
const config: SocketIoConfig = { url: 'http://localhost:8081', options: {} };
@NgModule({
...
imports: [
...
SocketIoModule.forRoot(config),
...
],
..
})

最新更新