简体   繁体   中英

WebSocket in Plain Java

I am coding a Web Framework for me with an WebServer and an WebSocket Server.

My Current Problem since days is, that the Response Content of my WebSocket Client is very funny...

It sends me not the Content as bytes, every time the value is another.

Web Response for normal HTTP and the Socket request works perfectly.

My current Code:

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

Regards Jan

Fixed getting raw bytes with following Code:

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

                                inputIO.read(bufferIO);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM