纯 WebSocket Java

WebSocket in Plain Java

我正在使用 WebServer 和 WebSocket Server 为我编写 Web 框架。

我最近的问题是,我的 WebSocket 客户端的响应内容非常有趣...

它发送给我的不是字节内容,每次值都是另一个。

正常 HTTP 和 Socket 请求的 Web 响应完美无缺。

我当前的代码:

                            poolIO.execute(new Thread() {
                            @Override
                            public void run() {
                                try {
                                    InputStream inputIO = clientIO.getInputStream();
                                    StringBuilder builderIO = new StringBuilder();

                                    while (clientIO.isConnected()) {

                                        int cacheIO = 0;
                                        int countIO = 0;
                                        byte[] binaryIO = new byte[0];


                                        while ((inputIO.available() != 0 && (cacheIO = inputIO.read()) != 0)) {
                                            binaryIO = new byte[binaryIO.length + 1];
                                            binaryIO[countIO] = (byte) cacheIO;
                                            builderIO.append((char) cacheIO);
                                            countIO++;
                                        }


                                        if (builderIO.length() > 0) {
                                            string(clientIO, builderIO.toString());
                                            binary(clientIO, binaryIO);
                                            binaryIO = new byte[0];
                                            builderIO.delete(0, builderIO.length());
                                        }
                                    }

                                    inputIO.close();

                                    disconnect(clientIO);

                                    this.stop();
                                } catch (IOException errorIO) {
                                    if (errorIO.getMessage().equalsIgnoreCase("Stream closed.")) {
                                        logIO.debug(GuardianLog.Type.INFO, "Client with IP " + clientIO.getInetAddress().getHostAddress() + " disconnected from Server with Port " + networkIO.getPort() + ".");
                                    } else logIO.append(GuardianLog.Type.ERROR, "Socket throw an Error ", errorIO);
                                }
                            }
                        });

问候 一月

修复了使用以下代码获取原始字节的问题:

      byte[] bufferIO = new byte[inputIO.available()];

                                inputIO.read(bufferIO);

                                if (bufferIO.length != 0) {
                                    binary(clientIO, bufferIO);
                                    string(clientIO, new String(bufferIO));
                                }