Hello All,

I am trying to connect to Erlang based server using Angular 6 client in
socket.io-client. I am able to connect with the server, but not able to get
any type of response from server after emitting data to server.

I am attaching my code snippet below.

wesocket.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);
}
}


Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to