This commit is contained in:
@@ -15,6 +15,7 @@ import com.wentch.redkale.net.http.*;
|
|||||||
import com.wentch.redkale.net.sncp.*;
|
import com.wentch.redkale.net.sncp.*;
|
||||||
import com.wentch.redkale.service.*;
|
import com.wentch.redkale.service.*;
|
||||||
import com.wentch.redkale.util.*;
|
import com.wentch.redkale.util.*;
|
||||||
|
import com.wentch.redkale.util.AnyValue.DefaultAnyValue;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -113,7 +114,15 @@ public final class NodeHttpServer extends NodeServer {
|
|||||||
mappings[i] = prefix + mappings[i];
|
mappings[i] = prefix + mappings[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.httpServer.addHttpServlet(servlet, en.getProperty(), mappings);
|
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
|
||||||
|
WebInitParam[] webparams = ws.initParams();
|
||||||
|
if (webparams.length > 0) {
|
||||||
|
if (servletConf == null) servletConf = new DefaultAnyValue();
|
||||||
|
for (WebInitParam webparam : webparams) {
|
||||||
|
servletConf.addValue(webparam.name(), webparam.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.httpServer.addHttpServlet(servlet, servletConf, mappings);
|
||||||
if (sb != null) sb.append(threadName).append(" Loaded ").append(clazz.getName()).append(" --> ").append(Arrays.toString(mappings)).append(LINE_SEPARATOR);
|
if (sb != null) sb.append(threadName).append(" Loaded ").append(clazz.getName()).append(" --> ").append(Arrays.toString(mappings)).append(LINE_SEPARATOR);
|
||||||
}
|
}
|
||||||
if (sb != null && sb.length() > 0) logger.log(Level.FINE, sb.toString());
|
if (sb != null && sb.length() > 0) logger.log(Level.FINE, sb.toString());
|
||||||
|
|||||||
24
src/com/wentch/redkale/net/http/WebInitParam.java
Normal file
24
src/com/wentch/redkale/net/http/WebInitParam.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface WebInitParam {
|
||||||
|
|
||||||
|
String name();
|
||||||
|
|
||||||
|
String value();
|
||||||
|
|
||||||
|
String description() default "";
|
||||||
|
}
|
||||||
@@ -23,4 +23,6 @@ public @interface WebServlet {
|
|||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
int moduleid() default 0;
|
int moduleid() default 0;
|
||||||
|
|
||||||
|
WebInitParam[] initParams() default {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.wentch.redkale.net.http;
|
package com.wentch.redkale.net.http;
|
||||||
|
|
||||||
|
import static com.wentch.redkale.net.http.WebSocketPacket.DEFAULT_PING_PACKET;
|
||||||
|
import static com.wentch.redkale.net.http.WebSocketServlet.DEFAILT_LIVEINTERVAL;
|
||||||
|
import com.wentch.redkale.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -19,8 +23,33 @@ public final class WebSocketEngine {
|
|||||||
|
|
||||||
private final Map<Serializable, WebSocketGroup> containers = new ConcurrentHashMap<>();
|
private final Map<Serializable, WebSocketGroup> containers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
protected WebSocketEngine(String engineid) {
|
private ScheduledThreadPoolExecutor scheduler;
|
||||||
|
|
||||||
|
protected final Logger logger;
|
||||||
|
|
||||||
|
protected final boolean finest;
|
||||||
|
|
||||||
|
protected WebSocketEngine(String engineid, Logger logger) {
|
||||||
this.engineid = engineid;
|
this.engineid = engineid;
|
||||||
|
this.logger = logger;
|
||||||
|
this.finest = logger.isLoggable(Level.FINEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(AnyValue conf) {
|
||||||
|
final int liveinterval = conf == null ? DEFAILT_LIVEINTERVAL : conf.getIntValue("liveinterval", DEFAILT_LIVEINTERVAL);
|
||||||
|
if (liveinterval == 0) return;
|
||||||
|
if (scheduler != null) return;
|
||||||
|
this.scheduler = new ScheduledThreadPoolExecutor(1, (Runnable r) -> {
|
||||||
|
final Thread t = new Thread(r, engineid + "-WebSocket-LiveInterval-Thread");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
long now = System.currentTimeMillis() / 1000;
|
||||||
|
long delay = liveinterval - now / liveinterval;
|
||||||
|
scheduler.scheduleWithFixedDelay(() -> {
|
||||||
|
getWebSocketGroups().stream().forEach(x -> x.getWebSockets().forEach(y -> y.send(DEFAULT_PING_PACKET)));
|
||||||
|
}, delay, liveinterval, TimeUnit.SECONDS);
|
||||||
|
if (finest) logger.finest(this.getClass().getSimpleName() + " start keeplive(interval:" + liveinterval + "s) scheduler executor");
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(WebSocket socket) {
|
void add(WebSocket socket) {
|
||||||
@@ -48,6 +77,7 @@ public final class WebSocketEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
|
if (scheduler != null) scheduler.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEngineid() {
|
public String getEngineid() {
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public final class WebSocketPacket {
|
public final class WebSocketPacket {
|
||||||
|
|
||||||
|
public static final WebSocketPacket DEFAULT_PING_PACKET = new WebSocketPacket(FrameType.PING, new byte[0]);
|
||||||
|
|
||||||
public static enum FrameType {
|
public static enum FrameType {
|
||||||
|
|
||||||
TEXT(0x01), BINARY(0x02), CLOSE(0x08), PING(0x09), PONG(0x0A);
|
TEXT(0x01), BINARY(0x02), CLOSE(0x08), PING(0x09), PONG(0x0A);
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ import javax.annotation.*;
|
|||||||
*/
|
*/
|
||||||
public abstract class WebSocketServlet extends HttpServlet implements Nameable {
|
public abstract class WebSocketServlet extends HttpServlet implements Nameable {
|
||||||
|
|
||||||
|
public static final String WEBPARAM__LIVEINTERVAL = "liveinterval";
|
||||||
|
|
||||||
|
public static final int DEFAILT_LIVEINTERVAL = 60;
|
||||||
|
|
||||||
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||||
|
|
||||||
private final MessageDigest digest = getMessageDigest();
|
private final MessageDigest digest = getMessageDigest();
|
||||||
@@ -58,9 +62,10 @@ public abstract class WebSocketServlet extends HttpServlet implements Nameable {
|
|||||||
@Override
|
@Override
|
||||||
public void init(Context context, AnyValue conf) {
|
public void init(Context context, AnyValue conf) {
|
||||||
InetSocketAddress addr = context.getServerAddress();
|
InetSocketAddress addr = context.getServerAddress();
|
||||||
this.engine = new WebSocketEngine(addr.getHostString() + ":" + addr.getPort() + "-" + name());
|
this.engine = new WebSocketEngine(addr.getHostString() + ":" + addr.getPort() + "-" + name(), logger);
|
||||||
this.node.putWebSocketEngine(engine);
|
this.node.putWebSocketEngine(engine);
|
||||||
this.node.init(conf);
|
this.node.init(conf);
|
||||||
|
this.engine.init(conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user