0

i want to send event to socket io server that ran on java spring boot application, the weird point is that my angular app can connect to server perfectly and i can get connect and disconnect events in java.but when it comes to emit an event to server or get any event from server it is not working

Java Class:

public class SocketIOServerImpl {

    public static void main(String[] args) throws InterruptedException {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(8090);
websocket
        final SocketIOServer server = new SocketIOServer(config);

        server.addConnectListener(socketIOClient -> {
            System.out.println("User Connected");
            server.getBroadcastOperations().sendEvent("daniyal", "You Connected to Server Successfully");
        });

        server.addDisconnectListener(client -> {
            server.getBroadcastOperations().sendEvent("daniyal", "You Connected to Server Successfully");
        });

        server.addEventListener("daniyal", String.class, new DataListener<String>() {

            @Override
            public void onData(SocketIOClient socketIOClient, String s, AckRequest ackRequest) throws Exception {
                System.out.println("User Connected");
                server.getBroadcastOperations().sendEvent("daniyal", "You Emited STH to Server Successfully");
            }
        });

        server.start();
        Thread.sleep(Integer.MAX_VALUE);

        server.stop();
    }
}

angular servise.ts:

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

  readonly uri: string = 'ws://localhost:8090';
  socket: any;

  constructor() {
    this.socket = io.connect(this.uri,{ transports: [ 'websocket' ] });
  }


  listen(eventName: string) {
    return new Observable((subscriber) => {
      this.socket.on(eventName, (data: any) => {
        subscriber.next(data);
      });
    });
  }

  emit(eventName: string, data: any) {
    this.socket.emit(eventName, data);
  }


}

app.component.ts:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  constructor(private socketService: SocketServiceService) {
  }

  ngOnInit() {
    this.socketService.listen('daniyal').subscribe(
      (res) => {
        console.log('Server Response: ',res);
      });
  }

  emit(){
    this.socketService.emit('daniyal','HI SERVER');
  }
}

i use angular version 14 and java 11

i expected server can get the event and when send event to client,client get that too

3
  • You are trying to connect a WebSocket (from Angular) to a Java socket, which they don't use the same protocol. This can give you a start point Commented Nov 9, 2022 at 11:52
  • What you say is not correct. The programming language does not matter, since Client and Server are trying to connect through websocket protocol Commented Nov 9, 2022 at 12:31
  • @LeonardoEmmanueldeAzevedo i read that question and answer,the point is that i am using same library in both client and server(socket io) and in netty-socketio github repo shown an example made of java and java script and it is working perfectly,problem is when i use angular it is not working Commented Nov 9, 2022 at 12:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.