Hey everyone, just wanted to release my networking library as used in my MMO as open source. I know there’s many of these out there like KyroNet but I found this much easier to use (and maybe others will too?)
Link: https://github.com/Davidx1337/UxNetIO
License: MIT (do whatever you want basically)
It is based on the Apache MINA API (design wise) but i’ve removed many layers of abstraction that MINA provides as well as providing a backwards-compatible API for old raw java.net.Socket usage. This boils down to a 12kb jar file while MINA is over 300kb!
Example HTTP server (very basic):
public static void main(String[] argv)
throws Throwable {
SocketAcceptor acceptor = new SocketAcceptor(80, 10);
acceptor.bind(new Protocol() {
ProtocolEncoder encoder = new ProtocolEncoder() {
public void dispose() {
}
public void encode(IoSession session, Object message, EncoderOutput output) {
String str = (String) message;
str = "<div style=\"color:blue;\">" + str;
str += "</div>\n";
ByteBuffer bb = ByteBuffer.allocate(str.length());
bb.put(str.getBytes());
bb.flip();
output.write(bb);
}
};
ProtocolDecoder decoder = new ProtocolDecoder() {
public void dispose() {
}
public void decode(IoSession session, ByteBuffer message, DecoderOutput output) {
byte[] data = message.array();
output.write(new String(data));
}
};
public ProtocolEncoder getEncoder() {
return encoder;
}
public ProtocolDecoder getDecoder() {
return decoder;
}
}, new IoListener() {
public void onSessionCreated(IoSession session) {
System.err.println("SessionCreated");
}
public void onSessionOpened(IoSession session) {
System.err.println("SessionOpened");
}
public void onMessageSent(IoSession session, Object message) {
System.err.println("MessageSent: " + message);
}
public void onMessageReceived(IoSession session, Object message) {
System.err.println("MessageReceived: " + message);
for (int i = 0; i < 100; i++) {
StringBuilder b = new StringBuilder("Hello from UxNetIO! This is line:"+i);
session.write(b.toString());
}
session.close();
}
public void onSessionClosed(IoSession session) {
System.err.println("SessionClosed");
}
public void onExceptionOccurred(IoSession session, Throwable ex) {
ex.printStackTrace();
}
});
System.err.println("LISTENING ON PORT: " + acceptor.getPort());
}
Output: (Google Chrome)
One of the games utilising this library is my MMORPG Renoria (it worked for us so maybe it will work for someone else?):
Either way, I hope someone finds this useful!