Код:
Client::handlePacket
public void handlePacket(Packet packet) {
if(packet instanceof OutContentResponsePacket) {
OutContentResponsePacket content = (OutContentResponsePacket) packet;
BrowserController.controller.setContent(content.getContent());
channel.close();
statusChange.accept(false);
}
}
BrowserController::setContent
WebView content;
public void setContent(String s) {
Platform.runLater(() -> content.getEngine().loadContent(s));
}
Код в котором отправляется OutConnectResponsePacket (PacketManager::sendContents)
public static void sendContents(Channel channel, String contents) {
OutContentResponsePacket packet = new OutContentResponsePacket(contents);
ByteBuf buffer = channel.alloc().buffer();
PacketBuffer packetBuffer = new PacketBuffer(buffer);
packetBuffer.writeInt(packet.getID());
packet.write(packetBuffer);
channel.writeAndFlush(packetBuffer.getBuffer());
}
Метод который вызывает PacketManager::sendContents (Server::handlePacket)
public void handlePacket(Channel channel, Packet packet) throws Exception {
if(packet instanceof InContentRequestPacket) {
InContentRequestPacket contentRequest = (InContentRequestPacket) packet;
String content;
if(!Charset.isSupported(contentRequest.getCharset())) {
content = "[ERR:01] => Charset isn't supported.";
} else {
Charset charset = Charset.forName(contentRequest.getCharset());
ByteBuffer buffer = charset.encode(contentOf(contentRequest.getLink()));
content = new String(buffer.array());
}
PacketManager.sendContents(channel, content);
}
}
Server::contentOf
private String contentOf(String link) throws IOException {
File requestedFile = new File(host, link);
System.out.println(requestedFile.getAbsolutePath());
if(!requestedFile.exists()) {
return "[ERR:02] File not found.";
}
return new String(Files.readAllBytes(requestedFile.toPath()));
}