-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePacketLibraryTest.java
More file actions
68 lines (55 loc) · 2.04 KB
/
SimplePacketLibraryTest.java
File metadata and controls
68 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import com.luneruniverse.simplepacketlibrary.Client;
import com.luneruniverse.simplepacketlibrary.PacketRegistry;
import com.luneruniverse.simplepacketlibrary.Server;
import com.luneruniverse.simplepacketlibrary.packets.Packet;
import com.luneruniverse.simplepacketlibrary.packets.PrimitivePacket;
public class SimplePacketLibraryTest {
// Custom Packet
public static class NameRequestPacket extends Packet {
public NameRequestPacket() {
}
public NameRequestPacket(DataInputStream in) {
}
public void write(DataOutputStream out) {
}
}
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
// Tracks all the custom packets
PacketRegistry registry = new PacketRegistry();
registry.registerPacket(NameRequestPacket.class);
// Create the server & client
Server server = new Server(60500);
Client client = new Client(60500);
// Register all the custom packets
server.registerPackets(registry);
client.registerPackets(registry);
// Called when the client first connects
server.addConnectionListener((connection, wait) -> {
connection.sendPacket(new NameRequestPacket(), (packet, connection2, wait2) -> {
System.out.println("[Client -> Server] " + (String) ((PrimitivePacket) packet).getValue());
try {
// Stop the server when done
// Will cause the client to also close
server.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
});
// Called when the server sends a packet
client.addPacketListener((packet, connection, wait) -> {
System.out.println("[Server -> Client] " + packet.getClass().getName());
if (packet instanceof NameRequestPacket) {
client.reply(packet, new PrimitivePacket("alfred"));
}
});
// Start the server & client
server.start();
client.start();
}
}