使用Web套接字在angular和Java之间发送和接收数据(例如文本)

时间:2019-07-19 12:38:39

标签: angularjs angular sockets websocket java-websocket

我想建立一个后端到前端的连接以查看UI上的实时文本。 为此,我希望将angular作为前端,将java作为后端(由于我是套接字新手,现在不考虑使用spring)。

通信应该往返...即两端应该是接收方和发送方。

我曾尝试将https://www.geeksforgeeks.org/socket-programming-in-java/客户端和服务器用于极客。有用 现在,我希望我的角度应用程序成为接收方(帮助使其成为发送方也将很棒)

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

}

以上登录控制台,但没有任何内容发送到后端。

使用如下所示的发件人时:

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

连接服务器后输出以下消息,但没有任何提示:

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

0 个答案:

没有答案