IDEMPIERE-3840 Use web socket for server push. Fix initial connection setup. Fix handling of timeout.
This commit is contained in:
parent
16595cca41
commit
5d65cc419d
|
@ -82,6 +82,15 @@ public class ServerPushEndPoint {
|
||||||
|
|
||||||
@OnMessage
|
@OnMessage
|
||||||
public void onMessage(Session session, String message) {
|
public void onMessage(Session session, String message) {
|
||||||
|
if (session == this.session && !Util.isEmpty(message)) {
|
||||||
|
if (message.equals("__ping__")) {
|
||||||
|
try {
|
||||||
|
session.getBasicRemote().sendText("__pong__");
|
||||||
|
} catch (IllegalArgumentException | IOException e) {
|
||||||
|
CLogger.getCLogger(getClass()).log(Level.WARNING, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -62,6 +62,7 @@ public class WebSocketServerPush implements ServerPush {
|
||||||
private final Object _mutex = new Object();
|
private final Object _mutex = new Object();
|
||||||
|
|
||||||
private final static Map<String, ServerPushEndPoint> endPointMap = new ConcurrentHashMap<>();
|
private final static Map<String, ServerPushEndPoint> endPointMap = new ConcurrentHashMap<>();
|
||||||
|
private final static Map<String, Boolean> unregisterMap = new ConcurrentHashMap<>();
|
||||||
private final static ServerPushEndPoint STUB = new ServerPushEndPoint();
|
private final static ServerPushEndPoint STUB = new ServerPushEndPoint();
|
||||||
private List<Schedule<Event>> schedules = new ArrayList<>();
|
private List<Schedule<Event>> schedules = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -120,7 +121,7 @@ public class WebSocketServerPush implements ServerPush {
|
||||||
if (endPoint == null) {
|
if (endPoint == null) {
|
||||||
if (dt.isServerPushEnabled()) {
|
if (dt.isServerPushEnabled()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(5000);
|
Thread.sleep(2000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
endPoint = getEndPoint(dt.getId());
|
endPoint = getEndPoint(dt.getId());
|
||||||
|
@ -184,7 +185,8 @@ public class WebSocketServerPush implements ServerPush {
|
||||||
if (dt != null) {
|
if (dt != null) {
|
||||||
ServerPushEndPoint endPoint = getEndPoint(dt.getId());
|
ServerPushEndPoint endPoint = getEndPoint(dt.getId());
|
||||||
if (endPoint == null) {
|
if (endPoint == null) {
|
||||||
if (dt.isServerPushEnabled()) {
|
if (dt.isServerPushEnabled() && unregisterMap.remove(dt.getId(), Boolean.TRUE)) {
|
||||||
|
registerEndPoint(dt.getId(), STUB);
|
||||||
startServerPushAtClient(dt);
|
startServerPushAtClient(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,6 +288,9 @@ public class WebSocketServerPush implements ServerPush {
|
||||||
*/
|
*/
|
||||||
public static boolean unregisterEndPoint(String dtid) {
|
public static boolean unregisterEndPoint(String dtid) {
|
||||||
ServerPushEndPoint endpoint = endPointMap.remove(dtid);
|
ServerPushEndPoint endpoint = endPointMap.remove(dtid);
|
||||||
|
if (endpoint != null) {
|
||||||
|
unregisterMap.put(dtid, Boolean.TRUE);
|
||||||
|
}
|
||||||
return endpoint != null;
|
return endpoint != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
(function() {
|
(function() {
|
||||||
org.idempiere.websocket.startServerPush = function(dtid) {
|
org.idempiere.websocket.startServerPush = function(dtid) {
|
||||||
var dt = zk.Desktop.$(dtid);
|
var dt = zk.Desktop.$(dtid);
|
||||||
if (dt._serverpush)
|
if (dt._serverpush && dt._serverpush.socket) {
|
||||||
dt._serverpush.stop();
|
if (dt._serverpush._reconnectId) {
|
||||||
|
return;
|
||||||
var spush = new org.idempiere.websocket.ServerPush(dt);
|
}
|
||||||
|
dt._serverpush.restart();
|
||||||
|
} else {
|
||||||
|
var spush = new org.idempiere.websocket.ServerPush(dtid);
|
||||||
spush.start();
|
spush.start();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
org.idempiere.websocket.stopServerPush = function(dtid) {
|
org.idempiere.websocket.stopServerPush = function(dtid) {
|
||||||
var dt = zk.Desktop.$(dtid);
|
var dt = zk.Desktop.$(dtid);
|
||||||
|
@ -13,15 +17,20 @@
|
||||||
dt._serverpush.stop();
|
dt._serverpush.stop();
|
||||||
};
|
};
|
||||||
org.idempiere.websocket.ServerPush = zk.$extends(zk.Object, {
|
org.idempiere.websocket.ServerPush = zk.$extends(zk.Object, {
|
||||||
desktop: null,
|
$init: function(dtid) {
|
||||||
socket: null,
|
this.dtid = dtid;
|
||||||
active: false,
|
this.socket = null;
|
||||||
delay: 10,
|
this.active = false;
|
||||||
$init: function(desktop) {
|
this.reconnect = false;
|
||||||
this.desktop = desktop;
|
var desktop = zk.Desktop.$(this.dtid);
|
||||||
|
desktop._serverpush = this;
|
||||||
},
|
},
|
||||||
start: function() {
|
start: function() {
|
||||||
this.desktop._serverpush = this;
|
var desktop = zk.Desktop.$(this.dtid);
|
||||||
|
if (typeof desktop === 'undefined')
|
||||||
|
return;
|
||||||
|
this.reconnect = false;
|
||||||
|
this._reconnectId = null;
|
||||||
var url = window.location.protocol == "http:" ? "ws://" : "wss://";
|
var url = window.location.protocol == "http:" ? "ws://" : "wss://";
|
||||||
var path = window.location.href.substring(window.location.protocol.length+2);
|
var path = window.location.href.substring(window.location.protocol.length+2);
|
||||||
path = path.substring(location.host.length+1);
|
path = path.substring(location.host.length+1);
|
||||||
|
@ -31,32 +40,48 @@
|
||||||
} else {
|
} else {
|
||||||
path = "/serverpush/";
|
path = "/serverpush/";
|
||||||
}
|
}
|
||||||
url = url + window.location.host + path + this.desktop.id;
|
url = url + window.location.host + path + desktop.id;
|
||||||
var me = this;
|
var me = this;
|
||||||
this.socket = new WebSocket(url);
|
this.socket = new WebSocket(url);
|
||||||
this.socket.onopen = function (event) {
|
this.socket.onopen = function (event) {
|
||||||
me.active = true;
|
me.active = true;
|
||||||
|
me.socket.send("__ping__");
|
||||||
};
|
};
|
||||||
this.socket.onmessage = function (event) {
|
this.socket.onmessage = function (event) {
|
||||||
if (event.data=="echo") {
|
if (event.data=="echo") {
|
||||||
zAu.cmd0.echo(this.desktop);
|
var desktop = zk.Desktop.$(me.dtid);
|
||||||
|
zAu.cmd0.echo(desktop);
|
||||||
} else if (event.data=="stop") {
|
} else if (event.data=="stop") {
|
||||||
me.stop();
|
me.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.socket.onclose = function (event) {
|
||||||
|
if (me.socket) {
|
||||||
|
me.socket = null;
|
||||||
|
}
|
||||||
|
if (me.reconnect) {
|
||||||
|
me._reconnectId = setTimeout(function() {me.start();}, 2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
this.socket.onerror = function(event) {
|
this.socket.onerror = function(event) {
|
||||||
console.log(event);
|
console.log(event);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
stop: function() {
|
stop: function() {
|
||||||
this.active = false;
|
this.active = false;
|
||||||
this.desktop._serverpush = null;
|
var desktop = zk.Desktop.$(this.dtid);
|
||||||
|
desktop._serverpush = null;
|
||||||
if (this.socket) {
|
if (this.socket) {
|
||||||
try {
|
try {
|
||||||
this.socket.close();
|
this.socket.close(1000);
|
||||||
} catch (error) {}
|
} catch (error) {
|
||||||
this.socket = null;
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
restart: function() {
|
||||||
|
this.reconnect = true;
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
})();
|
})();
|
Loading…
Reference in New Issue