E
E
EnderBro3D2018-04-18 20:27:26
Java
EnderBro3D, 2018-04-18 20:27:26

Questions at the end of the text, WebView(JavaFX)?

5ad77d4a9a600361149269.png5ad77fb9b4dea803242053.png
The code:

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));
}

The code in which the OutConnectResponsePacket is sent (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());
}

Method that calls 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()));
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question