| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
An easy-to-use, event-driven, asynchronous, network application framework. SimpleNet provides a built-in Server class which the user can instantiate and bind to an existing IP address and port. The user can then create one or more Client instances and connect to their Server using the same IP address and port.
Maven:
<dependency>
<groupId>com.github.jhg023</groupId>
<artifactId>SimpleNet</artifactId>
<version>1.2.6</version>
</dependency>Gradle:
compile 'com.github.jhg023:SimpleNet:1.2.6'
module my.project {
requires SimpleNet;
}// Instantiate a new Client.
var client = new Client();
// Register a connection listener.
client.onConnect(() -> {
System.out.println(client + " has connected to the server!");
// Builds a packet and sends it to the server immediately.
Packet.builder().putByte(1).putInt(42).writeAndFlush(client);
});
// Register an optional pre-disconnection listener.
client.preDisconnect(() -> System.out.println(client + " is about to disconnect from the server!"));
// Register an optional post-disconnection listener.
client.postDisconnect(() -> System.out.println(client + " successfully disconnected from the server!"));
// Attempt to connect to a server AFTER registering listeners.
client.connect("localhost", 43594);// Instantiate a new Server.
var server = new Server();
// Register one connection listener.
server.onConnect(client -> {
System.out.println(client + " has connected!");
/*
* When one byte arrives from the client, switch on it.
* If the byte equals 1, then print an int when it arrives.
*
* Because `readByteAlways` is used, this will loop when
* the callback completes, but does not hang the main thread.
*/
client.readByteAlways(opcode -> {
switch (opcode) {
case 1:
client.readInt(System.out::println);
}
});
// Register an optional pre-disconnection listener.
client.preDisconnect(() -> System.out.println(client + " is about to disconnect!"));
// Register an optional post-disconnection listener.
client.postDisconnect(() -> System.out.println(client + " has successfully disconnected!"));
});
// Bind the server to an address and port AFTER registering listeners.
server.bind("localhost", 43594);| Back | FazBrowse Home | New Git URL |