i want to establish a backend to front end connection to view real time text on UI. For this i want angular as front end and java as backend (not considering spring right now as i am new to socketing).
Communication should be to and fro...i.e. both ends should be receiver and sender.
i have tried using https://www.geeksforgeeks.org/socket-programming-in-java/ the client and server on geeks for geeks. It works Now i want my angular application to be receiver (help for making it sender would be great too)
import {AfterViewInit, Component, OnInit} from '@angular/core';
// @ts-ignore
import * as io from 'socket.io-client';
@Component({
selector: 'app-server-component',
templateUrl: './server-component.component.html',
styleUrls: ['./server-component.component.scss']
})
export class ServerComponentComponent implements OnInit, AfterViewInit {
private socket: any;
public ngOnInit() {
this.socket = io("http://localhost:5000");
}
public ngAfterViewInit() { }
public move(action: string) {
this.socket.emit("move", action);
console.log(action);
}
}
Above logs on console but nothing is sent to backend.
when using the sender shown below:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Server {
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public Server(int port) {
// starts server and waits for a connection
try {
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("address: " + socket.getLocalPort());
System.out.println("Client accepted");
// takes input from the client socket
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (true) {
try {
System.out.println(in.readLine());
socket.getOutputStream().write("message-backend".getBytes("UTF-8"));
} catch (IOException i) {
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
} catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[]) { Server server = new Server(5000); }
}
Below is output when server is connected but nothing happends post that:
Server started Waiting for a client ... address: 5000 Client accepted GET /socket.io/?EIO=3&transport=polling&t=MmAFF5Q HTTP/1.1 Host: localhost:5000 Connection: keep-alive Accept: */* Origin: http://localhost:4200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36 Referer: http://localhost:4200/socket Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9