샘플 내용은 IntelliJ Community edition 을 기준으로 작성되었습니다.
IntelliJ 내부의 Maven 버전이 낮을 경우, 직접 Maven을 다운로드 받아서 설정해주셔야 합니다.
Maven 다운로드 : https://maven.apache.org/download.cgi
public class EchoServer {
private static final int PORT = 5056;
public static void main(String[] args) {
GameServerBootstrap bootstrap = new GameServerBootstrap();
bootstrap.application(new EchoServerApplication())
.option(UDPOption.THREAD_COUNT, 2)
.option(UDPOption.SO_RCVBUF, 1024)
.option(UDPOption.SO_SNDBUF, 1024)
.bind(PORT).start();
}
}
public class EchoServerApplication extends ServerApplication {
@Override
protected void setup() {
}
@Override
protected void tearDown() {
}
@Override
protected ClientPeer createPeer(InitialRequest initialRequest, NetworkPeer networkPeer) {
return new EchoPeer(initialRequest, networkPeer);
}
}
public class EchoMessage extends MessageBridge {
public static final short MESSAGE = 0;
public EchoMessage(RequestMessage request) {
super(request);
}
@FieldParameter(Code = MESSAGE)
public String message;
}
public class EchoPeer extends ClientPeer {
private static final Logger logger = LoggerFactory.getLogger(EchoPeer.class);
public EchoPeer(InitialRequest initialRequest, NetworkPeer networkPeer) {
super(initialRequest, networkPeer);
}
@Override
protected void onReceive(RequestMessage request, SendOptions options) {
EchoMessage message = new EchoMessage(request);
logger.info(MessageFormat.format("Client message is \"{0}\"", message.message));
ResponseMessage response = message.toResponse();
this.send(response, options);
}
@Override
protected void onDisconnect(DisconnectReason reason, String detail) {
}
}
_connection = new NetworkConnection();
_config = new ConnectionConfig
{
ChannelCount = 5,
DisconnectionTimeout = 3000,
IsCrcEnabled = false,
MaxUnreliableCommands = 0,
MTUSize = 1350,
PingInterval = 1000,
SentCountAllowance = 3,
WarningQueueSize = 500,
};
_connection.Configure(_config);
_connection.ResponseReceived += OnResponseReceived;
_connection.StatusChanged += OnStatusChanged;
...
const int MESSAGE_CODE = 0;
private static void OnResponseReceived(ResponseMessage response)
{
if (response.Code == MESSAGE_CODE)
{
string message = string.Empty;
if (response.Data.GetValue(MESSAGE_PARAM_CODE, out message))
{
Console.WriteLine("[OnResponseReceived] Server message is \"{0}\"", message);
}
}
}
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5056);
_connection.Connect(remoteEndPoint, new Version(0, 1, 0), null);
Thread receiveThread = new Thread(() =>
{
while (true)
{
_connection.NetworkUpdate();
}
});
receiveThread.Start();
const int MESSAGE_PARAM_CODE = 0;
public void Send(string input)
{
DataObject data = new DataObject();
data.SetString(MESSAGE_PARAM_CODE, input);
_connection.SendRequestMessage(MESSAGE_CODE, data, SendOptions.ReliableSend);
}