【Bug历练手册】Frame must be terminated with a null octet

stomp body 中文解析 问题

Spring 里边对于Stomp 通信解析通过StompEncoder实现,核心代码如下

    public byte[] encode(Map<String, Object> headers, byte[] payload) {
        Assert.notNull(headers, "'headers' is required");
        Assert.notNull(payload, "'payload' is required");

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length);
            DataOutputStream output = new DataOutputStream(baos);

            if (SimpMessageType.HEARTBEAT.equals(SimpMessageHeaderAccessor.getMessageType(headers))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Encoding heartbeat");
                }
                output.write(StompDecoder.HEARTBEAT_PAYLOAD);
            }

            else {
                StompCommand command = StompHeaderAccessor.getCommand(headers);
                if (command == null) {
                    throw new IllegalStateException("Missing STOMP command: " + headers);
                }

                output.write(command.toString().getBytes(StompDecoder.UTF8_CHARSET));
                output.write(LF);
                writeHeaders(command, headers, payload, output);
                output.write(LF);
                writeBody(payload, output);
                output.write((byte) 0);
            }

            return baos.toByteArray();
        }
        catch (IOException ex) {
            throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers,  ex);
        }
    }

字符集选用UTF-8解析

问题解决

出现这种问题主要是客户端序列化时对于UTF-8支持有问题, 之前我碰到的情况主要是 Socket Js客户端中,并未将web页面中的文字转化为UTF8格式

posted @ 2017-08-22 17:21  写昵称不如写代码  阅读(777)  评论(0编辑  收藏  举报