This commit is contained in:
Redkale
2017-05-21 21:21:56 +08:00
parent 45fe7cb3e9
commit 05925b4f78
5 changed files with 36 additions and 20 deletions

View File

@@ -36,8 +36,9 @@ import org.redkale.util.Comment;
* 详情见: https://redkale.org
*
* @author zhangjx
* @param <T> 泛型
*/
public abstract class WebSocket {
public abstract class WebSocket<T> {
@Comment("消息不合法")
public static final int RETCODE_SEND_ILLPACKET = 1 << 1; //2
@@ -79,6 +80,8 @@ public abstract class WebSocket {
JsonConvert _jsonConvert; //不可能为空
java.lang.reflect.Type _messageTextType; //不可能为空
private final long createtime = System.currentTimeMillis();
private Map<String, Object> attributes = new HashMap<>(); //非线程安全
@@ -528,17 +531,13 @@ public abstract class WebSocket {
public void onPong(byte[] bytes) {
}
public java.lang.reflect.Type getTextMessageType() {
return String.class;
}
public void onMessage(Object message) {
public void onMessage(T message) {
}
public void onMessage(byte[] bytes) {
}
public void onFragment(Object message, boolean last) {
public void onFragment(T message, boolean last) {
}
public void onFragment(byte[] bytes, boolean last) {

View File

@@ -99,9 +99,9 @@ public final class WebSocketGroup {
CompletableFuture<Integer> future = null;
for (WebSocket s : list) {
if (future == null) {
future = s.send(packet);
future = s.sendPacket(packet);
} else {
future.thenCombine(s.send(packet), (a, b) -> a | b);
future.thenCombine(s.sendPacket(packet), (a, b) -> a | (Integer) b);
}
}
return future == null ? CompletableFuture.completedFuture(0) : future;

View File

@@ -117,7 +117,7 @@ public class WebSocketRunner implements Runnable {
webSocket._group.setRecentWebSocket(webSocket);
try {
if (packet.type == FrameType.TEXT) {
Object message = convert.convertFrom(webSocket.getTextMessageType(), packet.receiveMasker, packet.receiveBuffers);
Object message = convert.convertFrom(webSocket._messageTextType, packet.receiveMasker, packet.receiveBuffers);
if (readBuffer != null) {
readBuffer.clear();
channel.read(readBuffer, null, this);

View File

@@ -6,6 +6,7 @@
package org.redkale.net.http;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.*;
import java.security.*;
@@ -61,6 +62,27 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
@Resource(name = "$")
protected WebSocketNode node;
protected final Type messageTextType;
protected WebSocketServlet() {
Type msgtype = String.class;
try {
for (Method method : this.getClass().getDeclaredMethods()) {
if (!method.getName().equals("createWebSocket")) continue;
if (method.getParameterCount() > 0) continue;
Type rt = method.getGenericReturnType();
if (rt instanceof ParameterizedType) {
msgtype = ((ParameterizedType) rt).getActualTypeArguments()[0];
}
if (msgtype == Object.class) msgtype = String.class;
break;
}
} catch (Exception e) {
logger.warning(this.getClass().getName() + " not designate text message type on createWebSocket Method");
}
this.messageTextType = msgtype;
}
@Override
final void preInit(HttpContext context, AnyValue conf) {
InetSocketAddress addr = context.getServerAddress();
@@ -103,6 +125,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
}
final WebSocket webSocket = this.createWebSocket();
webSocket._engine = this.node.localEngine;
webSocket._messageTextType = this.messageTextType;
webSocket._jsonConvert = jsonConvert;
webSocket._remoteAddress = request.getRemoteAddress();
webSocket._remoteAddr = request.getRemoteAddr();