java websocket api_WebSocket 之 Java API | 學步園

本文详细介绍了Java WebSocket API中数据的接收过程,包括`onData`方法的处理逻辑,如何将网络数据复制到原始数据缓冲区,并通过`HttpUtils.merge`合并数据。在接收到WebSocket消息后,根据消息类型调用相应的处理方法,如文本消息存储到队列,关闭消息则关闭连接。代码中`connection.available()`用于检查通道中可用的数据量,但其具体实现原理未深入探讨。
摘要由CSDN通过智能技术生成

private final class WebSocketProtocolHandler implements IConnectHandler, IDataHandler, IDisconnectHandler {

// network data

private ByteBuffer rawBuffer = null;

public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {

........

return true;

}

public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException {

if (connection.isOpen()) {

// copying available network data into raw data buffer

int available = connection.available();

ByteBuffer[] data = null;

if (available > 0) {

data = connection.readByteBufferByLength(available);

}

onData(data);

}

return true;

}

void onData(ByteBuffer[] data) throws IOException {

if (data == null) {

if (rawBuffer == null) {

rawBuffer = ByteBuffer.allocate(0);

}

} else {

if (rawBuffer == null) {

rawBuffer = HttpUtils.merge(data);

} else {

rawBuffer = HttpUtils.merge(rawBuffer, data);

}

}

parse(rawBuffer);

if (!rawBuffer.hasRemaining()) {

rawBuffer = null;

}

}

void parse(ByteBuffer buffer) throws IOException {

while (buffer.hasRemaining()) {

WebSocketMessage msg = WebSocketMessage.parse(buffer);

if (msg == null) {

return;

} else {

if (msg.isTextMessage()) {

synchronized (inQueue) {

inQueueVersion++;

inQueue.add(msg);

inQueue.notifyAll();

}

synchronized (webSocketHandlerGuard) {

if (webSocketHandlerAdapter != null) {

webSocketHandlerAdapter.onMessage(WebSocketConnection.this);

}

}

} else if (msg.isCloseMessage()) {

if (isCloseMsgSent.get()) {

if (LOG.isLoggable(Level.FINE)) {

LOG.fine("[" + getId() + "] echo close msg reveived. Destroying connection");

}

writeMessageIgnoreClose(msg);

destroy();

// peer initiated close

} else {

if (LOG.isLoggable(Level.FINE)) {

LOG.fine("[" + getId() + "] close msg reveived. echoing it and destroying connection");

}

isCloseMsgSent.set(true);

writeMessageIgnoreClose(msg);

destroy();

}

} else {

if (LOG.isLoggable(Level.FINE)) {

LOG.fine("[" + getId() + "] binary message received. The ws draft does not longer allow binary messages. Ignoring it");

}

}

}

}

}

在這裡把數據從 onData 中收取下了並且進行轉換後存入到  inQueue 中。這裡已經知道它是怎麼收取數據了,就差最後一步,怎麼轉化數據,分離有效信息的。看到這幾行代碼,回想下自己原來寫的也可以這麼做就行了。(

wronged.gif)if (connection.isOpen()) {

// copying available network data into raw data buffer

int available = connection.available(); //這裡還沒有仔細查看,如果誰有時間查看了,希望也轉告我原理

ByteBuffer[] data = null;

if (available > 0) {

data = connection.readByteBufferByLength(available);

}

onData(data);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值