Example usage for org.springframework.web.socket WebSocketMessage getPayloadLength

List of usage examples for org.springframework.web.socket WebSocketMessage getPayloadLength

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketMessage getPayloadLength.

Prototype

int getPayloadLength();

Source Link

Document

Return the number of bytes contained in the message.

Usage

From source file:org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.java

public void sendMessage(WebSocketMessage<?> message) throws IOException {
    if (shouldNotSend()) {
        return;/*from w  ww .  ja  va 2  s  .c o m*/
    }

    this.buffer.add(message);
    this.bufferSize.addAndGet(message.getPayloadLength());

    do {
        if (!tryFlushMessageBuffer()) {
            if (logger.isTraceEnabled()) {
                String text = String.format(
                        "Another send already in progress: "
                                + "session id '%s':, \"in-progress\" send time %d (ms), buffer size %d bytes",
                        getId(), getTimeSinceSendStarted(), getBufferSize());
                logger.trace(text);
            }
            checkSessionLimits();
            break;
        }
    } while (!this.buffer.isEmpty() && !shouldNotSend());
}

From source file:org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.java

private boolean tryFlushMessageBuffer() throws IOException {
    if (this.flushLock.tryLock()) {
        try {//from w  w  w.j a  v  a2 s. co m
            while (true) {
                WebSocketMessage<?> message = this.buffer.poll();
                if (message == null || shouldNotSend()) {
                    break;
                }
                this.bufferSize.addAndGet(message.getPayloadLength() * -1);
                this.sendStartTime = System.currentTimeMillis();
                getDelegate().sendMessage(message);
                this.sendStartTime = 0;
            }
        } finally {
            this.sendStartTime = 0;
            flushLock.unlock();
        }
        return true;
    }
    return false;
}