我们可以使用 Angular 6 客户端套接字连接到 Erlang 套接字服务器吗?

Can we connect to Erlang socket server using Angular 6 client socket?

我正在尝试使用 socket.io-client 中的 Angular 6 客户端连接到基于 Erlang 的服务器。我能够连接到服务器,但在向服务器发送数据后无法从服务器获得任何类型的响应。

我在下面附上我的代码片段。

//websocket.service.ts

import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { Observable } from 'rxjs';
import * as io from 'socket.io-client';
import * as Rx from 'rxjs/Rx';
import { LocationInfo } from '../core/locationInfo.model';
import { Event } from '../core/event.enum';

@Injectable({
providedIn: 'root'
})

export class WebsocketService {

private socket:SocketIOClient.Socket;

constructor() {}

connect(): Rx.Subject<MessageEvent> {
this.socket = io.connect('localhost:3000');
// We define our observable which will observe any incoming messages
// from our socket.io server.
let observable = new Observable(observer => {
  console.log('Observable created')
    this.socket.on('connect', (data) => {
      console.log(data)
    })

    this.socket.on('message', (data) => {
      console.log("Received message from Websocket Server"+data)
      observer.next(data);
    })
    return () => {
      this.socket.disconnect();
    }
});

// We define our Observer which will listen to messages
// from our other components and send messages back to our
// socket server whenever the `next()` method is called.
let observer = {
    next: (data: Object) => {
        console.log(JSON.stringify(data))
        this.socket.emit('join',JSON.stringify('Hiiii'));
        this.socket.send(JSON.stringify('Hiiii'))
    },
};

// we return our Rx.Subject which is a combination
// of both an observer and observable.
return Rx.Subject.create(observer, observable);
}
}

//app.component.ts

constructor(private liveLocServ: getLocationService) 
{}
ngOnInit() {
 this.liveLocServ.messages.subscribe(msg => {
  console.log(msg);
 })
}

//getLocation.service.ts

 import { Injectable } from '@angular/core';
 import { WebsocketService } from './websocket.service';
 import { Subject } from 'rxjs/Rx';
 import { map } from 'rxjs/operators';

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

messages: Subject<any>;

// Our constructor calls our wsService connect method
constructor(private wsService: WebsocketService) {
this.messages = <Subject<any>>wsService
  .connect()
  .pipe(map((response: any): any => {
    return response;
  }))
}

// Our simplified interface for sending
// messages back to our socket.io server
sendMsg(input) {
 this.messages.next(input);
}

}

我在 angular 6 中找到了使用 websocket 客户端连接 erlang 服务器的解决方案。所以在 erlang 服务器上应该实现 websocket 服务器。