Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
package project;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
extends Frame
implements KeyListener,WindowListener {
String Lampe = "Off";
BufferedReader BR;
TextArea textArea;
Socket soc;
String message;
public static void main(String[] args)
{
Client wnd = new Client();
}
public Client()
{
setVisible(true);
setSize(300,600);
textArea = new TextArea();
//textArea.setBackground(Color.white);
//textArea.setEditable(true);
textArea.setPreferredSize(new Dimension(290,20));
//textArea.setText("Hier erhalten Sie Informationen");
add(textArea);
setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
setBackground(Color.gray);
show();
addKeyListener(this);
addWindowListener(this);
try {
soc = new Socket("localhost",63190);
while (true){
textArea.append(readFromSocket()+"\n");
} }catch (UnknownHostException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
try {
BR.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paint( Graphics g)
{
if(Lampe == "Off"){
g.drawOval(80,100,40,40);
g.drawLine(100,100,100,120);
g.fillOval(160,120,20,20);
}
else if (Lampe == "On"){
g.drawOval(80,100,40,40);
g.drawLine(100,120,120,120);
g.setColor(Color.red);
g.fillOval(160,120,20,20);
}
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent event) {
// TODO Auto-generated method stub
int key = event.getKeyCode();
if (key == KeyEvent.VK_F1) {
Lampe = "Off";
writeToSocket(Lampe);
}
else if (key == KeyEvent.VK_F2) {
Lampe = "On";
writeToSocket(Lampe);
}
repaint();
}
public void keyReleased(KeyEvent arg0) {}
private void writeToSocket(String str){
PrintWriter PW;
try {
PW = new PrintWriter(soc.getOutputStream(),true);
PW.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String readFromSocket(){
try {
BR = new BufferedReader(new InputStreamReader(soc.getInputStream()));
while(true){
message=BR.readLine();
System.out.println("message: "+message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BR.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return message;
}
public void windowOpened(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
writeToSocket("Bye");
setVisible(false);
dispose();
}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
}
package project;
import java.io.*;
import java.net.*;
/**
*
*
*/
public class Server {
private Socket client = null;
private ServerSocket server;
private BufferedReader in;
private PrintWriter out;
public Server()
{
System.out.println("Warte auf Verbindungen...");
try {
server = new ServerSocket(63190);
while(true){
client = server.accept();
System.out.println("Verbindung hergestellt");
try {
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
System.out.println(inputLine);
if (inputLine == "Bye") {
break;
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
public void close() throws IOException
{
client.close();
server.close();
}
public static void main(String[] args) throws IOException
{
Server wnd1 = new Server();
}
}