Skip to content

Commit b3af35e

Browse files
committed
updated README.md
1 parent 87fa3bf commit b3af35e

File tree

1 file changed

+143
-1
lines changed

1 file changed

+143
-1
lines changed

README.md

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,148 @@ Download the latest jar file from the releases tab and implement it in your proj
88

99
### Example
1010

11+
First, we need to create the server and the client in for example the main method and call the connect method on both. We're also going to add a shutdown hook to disconnect both again. The packets need to be registered before the server or the client start. The packets must be always registered in the same order, on both the server and the client-side.
1112

13+
Server:
1214

13-
Let's start by creating two packets, one for requesting the current time and one for actually returning it. We do this by creating the classes and extending it from Packet. After we did this, we need to implement the methods and the constructor. Note that the default constructor has to be there because it is used for system recognition. You can implement as many custom constructors as you like, but inside them, you need to always set the connectionUUID to null in the super call. In the send method, you just write every variable you want to send to the writingByteBuffer and in the recieve method you initialize them again reading them from the readingByteBuffer. You must read and write the variables in the same order because otherwise, the system couldn't handle it correctly.
15+
```java
16+
package de.javasocketapitest.explanation;
17+
18+
import de.javasocketapi.core.PacketRegistry;
19+
import de.javasocketapi.core.Server;
20+
21+
import java.io.IOException;
22+
23+
public class ServerMain {
24+
25+
private static Server server;
26+
27+
public static Server getServer() {
28+
return server;
29+
}
30+
31+
public static void main(String[] args) throws IOException {
32+
PacketRegistry.registerPacket(RequestTimePacket.class);
33+
PacketRegistry.registerPacket(ReturnTimePacket.class);
34+
35+
ServerMain.server = new Server(19503);
36+
ServerMain.server.connect();
37+
38+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
39+
try {
40+
ServerMain.server.disconnect();
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
}));
45+
}
46+
}
47+
```
48+
49+
Client:
50+
51+
```java
52+
package de.javasocketapitest.explanation;
53+
54+
import de.javasocketapi.core.Client;
55+
import de.javasocketapi.core.PacketRegistry;
56+
57+
import java.io.IOException;
58+
59+
public class ClientMain {
60+
61+
private static Client client;
62+
63+
public static Client getClient() {
64+
return client;
65+
}
66+
67+
public static void main(String[] args) throws IOException {
68+
PacketRegistry.registerPacket(RequestTimePacket.class);
69+
PacketRegistry.registerPacket(ReturnTimePacket.class);
70+
71+
ClientMain.client = new Client("localhost", 19503);
72+
ClientMain.client.connect();
73+
74+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
75+
try {
76+
ClientMain.client.disconnect();
77+
} catch (IOException e) {
78+
e.printStackTrace();
79+
}
80+
}));
81+
82+
ClientMain.client.send(new RequestTimePacket(ClientMain.client.getConnectionUUID().get()));
83+
}
84+
}
85+
```
86+
87+
Let's continue by creating two packets, one for requesting the current time and one for actually returning it. We do this by creating the classes and extending it from Packet. After we did this, we need to implement the methods and the constructor. Note that the default constructor has to be there because it is used for system recognition. You can implement as many custom constructors as you like, but inside them, you need to always set the connectionUUID to null in the super call. In the send method, you just write every variable you want to send to the writingByteBuffer and in the recieve method you initialize them again reading them from the readingByteBuffer. You must read and write the variables in the same order because otherwise, the system couldn't handle it correctly.
88+
89+
RequestTimePacket:
90+
91+
```java
92+
package de.javasocketapitest.explanation;
93+
94+
import de.javasocketapi.core.Packet;
95+
import de.javasocketapi.core.ReadingByteBuffer;
96+
import de.javasocketapi.core.WritingByteBuffer;
97+
98+
import java.util.UUID;
99+
100+
public class RequestTimePacket extends Packet {
101+
public RequestTimePacket(UUID connectionUUID) {
102+
super(connectionUUID);
103+
}
104+
105+
@Override
106+
public void send(WritingByteBuffer writingByteBuffer) {
107+
108+
}
109+
110+
@Override
111+
public void recieve(ReadingByteBuffer readingByteBuffer) {
112+
ServerMain.getServer().sendToClient(new ReturnTimePacket(System.currentTimeMillis()), getConnectionUUID());
113+
}
114+
}
115+
```
116+
117+
ReturnTimePacket:
118+
119+
```java
120+
package de.javasocketapitest.explanation;
121+
122+
import de.javasocketapi.core.Packet;
123+
import de.javasocketapi.core.ReadingByteBuffer;
124+
import de.javasocketapi.core.WritingByteBuffer;
125+
126+
import java.util.UUID;
127+
128+
public class ReturnTimePacket extends Packet {
129+
130+
private long currentTimeMillis;
131+
132+
public ReturnTimePacket(UUID connectionUUID) {
133+
super(connectionUUID);
134+
}
135+
136+
public ReturnTimePacket(long currentTimeMillis) {
137+
super(null);
138+
this.currentTimeMillis = currentTimeMillis;
139+
}
140+
141+
@Override
142+
public void send(WritingByteBuffer writingByteBuffer) {
143+
writingByteBuffer.writeLong(this.currentTimeMillis);
144+
}
145+
146+
@Override
147+
public void recieve(ReadingByteBuffer readingByteBuffer) {
148+
this.currentTimeMillis = readingByteBuffer.readLong();
149+
150+
System.out.println("current time millis: " + this.currentTimeMillis);
151+
}
152+
}
153+
```
154+
155+
When you want to send a packet to all clients, you can simply use the method server.sendToAllClients. Or if you want to disconnect all clients, you can simply call server.disconnectAllCLients.

0 commit comments

Comments
 (0)