This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package com.wentch.redkale.net.http;
|
||||
|
||||
import com.wentch.redkale.net.*;
|
||||
import com.wentch.redkale.service.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
@@ -164,8 +165,17 @@ public abstract class WebSocket {
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocketBinary模式流程顺序: onOpen、createGroupid、onRead
|
||||
* WebSocket流程顺序: onOpen、createGroupid、onConnected、onMessage/onFragment+、onClose
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param channel
|
||||
*/
|
||||
public void onRead(AsyncConnection channel) {
|
||||
}
|
||||
|
||||
public void onConnected() {
|
||||
}
|
||||
|
||||
|
||||
23
src/com/wentch/redkale/net/http/WebSocketBinary.java
Normal file
23
src/com/wentch/redkale/net/http/WebSocketBinary.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.wentch.redkale.net.http;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 被标记为 @WebSocketBinary 的WebSocketServlet 将使用原始的TCP传输, 通常用于类似音频/视频传输场景
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@Inherited
|
||||
@Documented
|
||||
@Target({TYPE})
|
||||
@Retention(RUNTIME)
|
||||
public @interface WebSocketBinary {
|
||||
|
||||
}
|
||||
@@ -42,11 +42,14 @@ public class WebSocketRunner implements Runnable {
|
||||
|
||||
private final BlockingQueue<byte[]> queue = new ArrayBlockingQueue(1024);
|
||||
|
||||
public WebSocketRunner(Context context, WebSocket webSocket, AsyncConnection channel) {
|
||||
private final boolean wsbinary;
|
||||
|
||||
public WebSocketRunner(Context context, WebSocket webSocket, AsyncConnection channel, final boolean wsbinary) {
|
||||
this.context = context;
|
||||
this.engine = webSocket.engine;
|
||||
this.webSocket = webSocket;
|
||||
this.channel = channel;
|
||||
this.wsbinary = wsbinary;
|
||||
webSocket.runner = this;
|
||||
this.coder.logger = context.getLogger();
|
||||
this.coder.debugable = context.getLogger().isLoggable(Level.FINEST);
|
||||
@@ -62,6 +65,10 @@ public class WebSocketRunner implements Runnable {
|
||||
webSocket.onConnected();
|
||||
channel.setReadTimeoutSecond(300); //读取超时5分钟
|
||||
if (channel.isOpen()) {
|
||||
if (wsbinary) {
|
||||
webSocket.onRead(channel);
|
||||
return;
|
||||
}
|
||||
channel.read(readBuffer, null, new CompletionHandler<Integer, Void>() {
|
||||
|
||||
private ByteBuffer recentExBuffer;
|
||||
|
||||
@@ -34,6 +34,9 @@ public abstract class WebSocketServlet extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
//是否用于二进制流传输
|
||||
protected final boolean wsbinary = getClass().getAnnotation(WebSocketBinary.class) != null;
|
||||
|
||||
@Resource
|
||||
protected WebSocketNodeService nodeService;
|
||||
|
||||
@@ -102,7 +105,7 @@ public abstract class WebSocketServlet extends HttpServlet {
|
||||
}
|
||||
webSocket.groupid = groupid;
|
||||
engine.add(webSocket);
|
||||
context.submit(new WebSocketRunner(context, webSocket, response.removeChannel()));
|
||||
context.submit(new WebSocketRunner(context, webSocket, response.removeChannel(), wsbinary));
|
||||
response.finish(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user