jfinal 整合websocket 的完整实现 java

本文介绍如何使用Java和Maven在Tomcat环境下搭建WebSocket聊天机器人应用,包括环境配置、代码实现及测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景为用户发送 信息到服务器,服务器接收后,智能回复。用于聊天机器人。

1.maven环境加包
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-websocket-api</artifactId>
<version>7.0.47</version>
<scope>provided</scope>
</dependency>

2.Jfinal 配置文件修改(DemoConfig extends JFinalConfig )

public void configHandler(Handlers me) {
        //me.add(new ContextPathHandler("contextPath"));
        me.add(new WebSocketHandler("^/websocket")) 
    }

3.WebSocketHandler 文件
public class WebSocketHandler extends Handler{
private Pattern filterUrlRegxPattern;
    
    public WebSocketHandler(String filterUrlRegx) {
        if (StrKit.isBlank(filterUrlRegx))
            throw new IllegalArgumentException("The para filterUrlRegx can not be blank.");
        filterUrlRegxPattern = Pattern.compile(filterUrlRegx);
    }
    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        if (filterUrlRegxPattern.matcher(target).find())
            return ;
        else
            next.handle(target, request, response, isHandled);
        
    }
}

4.接收信息发送信息

@ServerEndpoint("/websocket")
public class WebSocketTest {
    public static CorpusService srv=CorpusService.me;
     //静态变量,用来记录当前在线连接数。
    private static AtomicInteger onlineCount = new AtomicInteger(0);

    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    //protected static CopyOnWriteArraySet<WebSocketTest> webSocketSet = new CopyOnWriteArraySet<WebSocketTest>();
    private static ConcurrentHashMap<String, WebSocketTest> webSocketSet = new ConcurrentHashMap<String, WebSocketTest>();
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;

    public WebSocketTest() {
        System.out.println(" WebSocket init~~~");
    }
    
    /**
     * 连接建立成功调用的方法
     * @param session  可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(Session session){
         System.out.println("新客人为" + session.getId());
        this.session = session;
       // webSocketSet.add(this);     //加入set中
        webSocketSet.put(session.getId(), this);//加入map中
        addOnlineCount();           //在线数加1
        System.out.println("有新连接"+session.getId()+"加入!当前在线人数为" + getOnlineCount());
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(){
        webSocketSet.remove(this);  //从set中删除
        subOnlineCount();           //在线数减1
        System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     * @throws IOException 
     * @throws InterruptedException 
     */
    @OnMessage
    public void onMessage(String message, Session session) throws IOException, InterruptedException {
        System.out.println("来自客户端的消息111:" + message);
        //群发消息
        getMessage(message,session);
        //if (webSocketSet.get(session.getId()) != null) {
        //    webSocketSet.get(session.getId()).sendMessage("用户" + session.getId() + "发来消息:" + "<br/> " + message);
        //}
        //session.getAsyncRemote().sendText("##"+message);
        //for(WebSocketTest item: webSocketSet){
         //   try {
         //       item.sendMessage(session.getId()+"说:"+message);
         //   } catch (IOException e) {
          //      e.printStackTrace();
          //      continue;
           // }
       // }
    }

    /**
     * 发生错误时调用
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error){
        System.out.println("发生错误");
        error.printStackTrace();
    }

    /**
     * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException{
        this.session.getBasicRemote().sendText(message);
        //this.session.getAsyncRemote().sendText(message);
    }

    public static int getOnlineCount() {
        return onlineCount.get();
    }

    public static void addOnlineCount() {
        onlineCount.incrementAndGet();
    }

    public static void subOnlineCount() {
        onlineCount.decrementAndGet();
    }
    
    public void getMessage(String msg,Session session) throws InterruptedException{
     //获取数据  

   AiCorpus cor=srv.getCorpus(msg);
        if(cor!=null){
            String msgs=cor.getMessage();
            String keywords=cor.getUsekeywords();
            if(msgs.contains("/r/n")){
                String[] strs=msgs.split("/r/n");
                for(String str :strs){
                    if (webSocketSet.get(session.getId()) != null &&!"".equals(str)) {
                        try {
                            webSocketSet.get(session.getId()).sendMessage(str);
                             Thread.sleep(500);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }else
            {
                if (webSocketSet.get(session.getId()) != null &&!"".equals(msgs)) {
                    try {
                        webSocketSet.get(session.getId()).sendMessage(msgs);
                         Thread.sleep(500);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (webSocketSet.get(session.getId()) != null &&!"".equals(keywords)) {
                try {
                    webSocketSet.get(session.getId()).sendMessage("-->"+keywords);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }else{
            try {
                webSocketSet.get(session.getId()).sendMessage("系统支持的词语:赠险是什么,保险是");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
}

5.测试工具:
http://coolaf.com/tool/chattest

6.请注意 默认jetty 环境不支持 websockt  ,本人亲测 必在tomcat 8.5 下通过。

转载于:https://my.oschina.net/u/3207581/blog/1845701

### Spring Boot 整合 WebSocket 实现消息推送 #### 添加依赖项 为了在Spring Boot应用程序中启用WebSocket支持,需要添加`spring-boot-starter-websocket`作为项目的依赖之一。这可以通过Gradle或Maven来完成。 对于使用Groovy编写的构建脚本,在dependencies闭包内加入如下语句[^1]: ```groovy implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' ``` 而对于XML风格的POM文件,则应包含这样的片段[^3]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` #### 配置WebSocket消息代理 配置类用于设置WebSocket的消息代理和其他必要的参数。通过继承`WebSocketConfigurer`接口并重写相应的方法可以自定义WebSocket的行为。此外,还可以注册一个`SockJS`端点以便于浏览器客户端连接。 #### 创建控制器处理请求 创建一个新的控制器类用来接收来自前端页面发出的HTTP升级握手请求以及后续的数据帧交换。此控制器应当标注有`@Controller`注解,并且内部方法需带有`@MessageMapping`和`@SendTo`等注解以指定路径映射关系及响应目标地址[^2]。 下面是一个简单的例子展示如何发送广播通知给所有已订阅特定主题的用户: ```java @Controller public class GreetingController { @MessageMapping("/hello") @SendTo("/topic/greetings") public String greeting(String message) throws Exception { Thread.sleep(1000); // simulated delay return "Hello, " + HtmlUtils.htmlEscape(message); } } ``` 这段代码中的`greeting()`函数会在接收到发往`/app/hello`的消息时被调用;而返回的结果则会被转发至`/topic/greetings`频道上的每一个监听者那里。 #### 启动定时任务进行消息推送 如果希望定期向在线用户推送更新信息,可以在应用里设定计划执行的任务。借助Spring框架自带的支持——即`@Scheduled`注解配合`SchedulingConfigurer`接口实现灵活的时间调度机制,从而达到周期性触发事件的效果。 例如,每五秒一次地向名为“time”的目的地发布当前时间戳: ```java @Component public class TimePusher implements SchedulingConfigurer { private final SimpMessagingTemplate messagingTemplate; public TimePusher(SimpMessagingTemplate messagingTemplate){ this.messagingTemplate = messagingTemplate; } @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask( () -> messagingTemplate.convertAndSend("/topic/time", new Date()), triggerContext -> { Calendar nextExecutionTime = new GregorianCalendar(); nextExecutionTime.add(Calendar.SECOND, 5); return nextExecutionTime.getTime(); }); } } ``` 以上就是利用Spring Boot整合WebSocket来进行即时通讯的一个基本流程概述。实际开发过程中可能还会涉及到更多细节调整和技术选型考量,比如安全性增强措施、性能优化策略等方面的内容[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值