Jetty中WebSocketListener的轻量级IPC

Jetty中WebSocketListener的轻量级IPC,websocket,jetty,ipc,jetty-9,java-websocket,Websocket,Jetty,Ipc,Jetty 9,Java Websocket,Android、iOS和桌面浏览器客户端目前每隔几秒钟轮询一次PHP后端(在CentOS Linux上使用PostgreSQL数据库) 我想用standalone来代替轮询,通知客户端新的数据可以在后端获取 因此,在自定义WebSocketListener中,我对连接的客户端进行身份验证,并将它们存储在ConcurrentHashMap中: 我的问题:如何通知已连接(通过WebSocket)的客户端 也就是说,在PHP脚本中,我想运行一个轻量级程序java-jar MyNotify.jar cl

Android、iOS和桌面浏览器客户端目前每隔几秒钟轮询一次PHP后端(在CentOS Linux上使用PostgreSQL数据库)

我想用standalone来代替轮询,通知客户端新的数据可以在后端获取

因此,在自定义
WebSocketListener
中,我对连接的客户端进行身份验证,并将它们存储在
ConcurrentHashMap
中:

我的问题:如何通知已连接(通过WebSocket)的客户端

也就是说,在PHP脚本中,我想运行一个轻量级程序
java-jar MyNotify.jar client-1234
,告诉Jetty独立服务器:

嘿,数据库中有关于
客户机-1234
的新数据

请通过websockets向其发送短消息
MyMap.get(“client-1234”).getRemote().sendString(“嘿”,null)

你必须把你的

ConcurrentHashMap<String,Session> sessionMap. 
然后,在应用程序中的任何地方,您都可以以正常方式(使用点语法)访问此静态字段

因为contextInitialized是在任何servlet或websockets方法(get、put、onMessage)之前激发的,所以map将在那里。同样是并发映射,它内部应该没有重复的id

当然,您还需要清理会话映射的策略。总之,您必须使用javax.servlet API中的事件来构建系统

类似的例子:

package example;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;

/**
 * Application lifecycle events. Handles:
 * <ul>
 *  <li>start, shutdown of application
 *  <li>start, stop of session
 * </ul>
 * 
 * @author mitjag
 *
 */
public class AppInit implements HttpSessionListener, ServletContextListener {

    public static final Logger log = Logger.getLogger(AppInit.class.getCanonicalName());

    public static final Map<String, HttpSession> SESSION_MAP = new ConcurrentHashMap<String, HttpSession>(); /* access AppInit.SESSION_MAP from anywhere in your app*/

    @Override
    public void contextInitialized(ServletContextEvent ctx) {}

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // With this trick we maintain the list of sessionid's together with corresponding session
        // It is used to grab the session if you have the valid session id
        final String sid = arg0.getSession().getId();
        log.info("SESSION CREATED with id " + arg0.getSession().getId());
        SESSION_MAP.put(sid, arg0.getSession());
    }

    /** 
     * Called on session invalidation (manual or session timeout trigger, defined in web.xml (session-timeout)).
     * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // remove session from our list (see method: sessionCreated)
        final String sid = arg0.getSession().getId();
        SESSION_MAP.remove(sid);
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

}
包示例;
导入java.io.FileNotFoundException;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入java.util.concurrent.ConcurrentHashMap;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.servlet.ServletContextEvent;
导入javax.servlet.ServletContextListener;
导入javax.servlet.http.*;
/**
*应用程序生命周期事件。处理:
*
    *
  • 启动、关闭应用程序 *
  • 会话的开始、停止 *
* *@作者mitjag * */ 公共类AppInit实现HttpSessionListener、ServletContextListener{ 公共静态最终记录器log=Logger.getLogger(AppInit.class.getCanonicalName()); public static final Map SESSION_Map=new ConcurrentHashMap();/*从应用程序中的任何位置访问AppInit.SESSION_Map*/ @凌驾 public void contextInitialized(ServletContextEvent ctx){} @凌驾 已创建公共无效会话(HttpSessionEvent arg0){ //使用此技巧,我们将会话ID列表与相应的会话一起维护 //如果您具有有效的会话id,它用于抓取会话 最后一个字符串sid=arg0.getSession().getId(); log.info(“使用id+arg0.getSession().getId()创建的会话”); SESSION_MAP.put(sid,arg0.getSession()); } /** *在会话无效时调用(手动或会话超时触发器,在web.xml(会话超时)中定义)。 *@see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent) */ @凌驾 public void sessionDestroyed(HttpSessionEvent arg0){ //从列表中删除会话(请参阅方法:sessionCreated) 最后一个字符串sid=arg0.getSession().getId(); 会话映射删除(sid); } @凌驾 公共无效上下文已销毁(ServletContextEvent arg0){ } }
  @Override
    public void contextInitialized(ServletContextEvent ctx) {
package example;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;

/**
 * Application lifecycle events. Handles:
 * <ul>
 *  <li>start, shutdown of application
 *  <li>start, stop of session
 * </ul>
 * 
 * @author mitjag
 *
 */
public class AppInit implements HttpSessionListener, ServletContextListener {

    public static final Logger log = Logger.getLogger(AppInit.class.getCanonicalName());

    public static final Map<String, HttpSession> SESSION_MAP = new ConcurrentHashMap<String, HttpSession>(); /* access AppInit.SESSION_MAP from anywhere in your app*/

    @Override
    public void contextInitialized(ServletContextEvent ctx) {}

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // With this trick we maintain the list of sessionid's together with corresponding session
        // It is used to grab the session if you have the valid session id
        final String sid = arg0.getSession().getId();
        log.info("SESSION CREATED with id " + arg0.getSession().getId());
        SESSION_MAP.put(sid, arg0.getSession());
    }

    /** 
     * Called on session invalidation (manual or session timeout trigger, defined in web.xml (session-timeout)).
     * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // remove session from our list (see method: sessionCreated)
        final String sid = arg0.getSession().getId();
        SESSION_MAP.remove(sid);
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

}