I have .net socket server , I successfuly connect to it using socket.io with angular ..but I cannot found how to send data to server and recieve from it ..can anyone help me
Work with Observables and write an Angular service with methods to listen and send messages to the server:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { Message } from '../model/message';
import * as socketIo from 'socket.io-client';
const SERVER_URL = 'https://yourserverhost.com/';
@Injectable()
export class SocketService {
private socket;
public initSocket(): void {
this.socket = socketIo(SERVER_URL);
}
public send(message: Message): void {
this.socket.emit('message', message);
}
public onMessage(): Observable<Message> {
return new Observable<Message>(observer => {
this.socket.on('message', (data: Message) => observer.next(data));
});
}
}
Find a complete app using WebSockets, Node.js and Angular written entirely in TypeScript here: https://github.com/luixaviles/socket-io-typescript-chat
I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With