Android,iOS和桌面瀏覽器客戶端每隔幾秒就會輪詢PHP後端(在CentOS Linux上使用PostgreSQL數據庫)。輕量級IPC到Jetty中的WebSocketListener
我想通過使用獨立的Jetty Websocket Server來替代輪詢來通知客戶端,新數據可用於在後端拾取。
所以在定製WebSocketListener
我驗證連接的客戶端,並將它們存儲在一個ConcurrentHashMap<String,Session>
:
public class MyListener implements WebSocketListener
{
private Session mSession;
@Override
public void onWebSocketConnect(Session session) {
mSession = session;
}
@Override
public void onWebSocketText(String message) {
if (mSession != null && mSession.isOpen()) {
// 1. validate client id and password
// 2. store client id and session into Map
}
}
我的問題:如何通知(透過WebSocket)連接的客戶端?
I.e.在PHP的腳本我想運行一個輕量級的程序java -jar MyNotify.jar client-1234
告訴碼頭獨立服務器:
嘿,有可用於
client-1234
在數據庫中的新數據!請致電
MyMap.get("client-1234").getRemote().sendString("hey", null);