java中Websocket的使用

什么试WebSocket

WebSocket是一个连接,这个连接是客户端(页面)与服务端之间的连接,处理两者间通讯;
好处:HTML5定义了WebSocket协议,能更好的节省服务器资源和带宽,并且能够更实时地进行通讯,不需要每次发送请求接口获取数据,
使用场景:比如客户端登录后,出现的消息推送,每天定时广播推送给客户端消息等场景;

SpringBoot maven项目中实现

导入依赖:

		<dependency>
		   <groupId>org.springframework.boot</groupId>
		   <artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>

客户端和服务端怎么链接呢?前端实现也是固定的写法,只需要请求/websocket/{userId} 这个地址即可实现链接

前端js代码:

//判断当前浏览器是否支持WebSocket  
if ('WebSocket' in window) {
    
      
    websocket = new WebSocket("ws://" + document.location.host + "/WebChat/websocket/" + username + "/"+ _img);  
} else {
    
      
    alert('当前浏览器 Not support websocket')  
} 

java 实现websocket连接的代码:

package org.jeecg.modules.message.websocket;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.WebsocketConst;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author wsf
 * @Date 2023/04/29 9:41
 * @Description: 此注解相当于设置访问URL
 */
@Component
@Slf4j
@ServerEndpoint("/websocket/{userId}") //此注解相当于设置访问URL
public class WebSocket {
    
    

    private Session session;

    private static final CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();
    /*
    websocket是客户端和服务端之间建立了一个连接,建立完连接以后,会生成一个websocket对象,我们可以用这个对象来执行发送,接收等操作。但是这只是一个存在于客户端与服务器之间的链接,换句话说,系统只能识别到这个websocket连接是对应于哪个页面(浏览器),而这个页面在系统中是对应哪个用户(数据库中的用户,或者根本就没有对应任何用户,即未登录,只是一个游客),我们是无法从这个websocket对象中获取的。所以我们需要创建一个Map对象,用于将websocket对象和实际的user对象进行关联,这样为我们后续向特定的用户推送消息做铺垫
    */
    private static final Map<String, Session> sessionPool = new HashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") String userId) {
    
    
        try {
    
    
            this.session = session;
            webSockets.add(this);
            sessionPool.put(userId, session);
            log.info("【websocket消息】有新的连接,总数为: {}", webSockets.size());
        } catch (Exception e) {
    
    
        }
    }

    @OnClose
    public void onClose(@PathParam(value = "userId") String userId) {
    
    
        try {
    
    
            webSockets.remove(this);
            sessionPool.remove(userId);
            log.info("【websocket消息】连接断开,总数为: {}", webSockets.size());
        } catch (Exception e) {
    
    
        }
    }

    @OnMessage
    public void onMessage(String message) {
    
    
        log.debug("【websocket消息】收到客户端消息: {}", message);
        JSONObject obj = new JSONObject();
        obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);//业务类型
        obj.put(WebsocketConst.MSG_TXT, "心跳响应");//消息内容
        session.getAsyncRemote().sendText(obj.toJSONString());
    }


    @OnError
    public void OnError(Session session, @PathParam(value = "userId") String userId, Throwable t) {
    
    
        try {
    
    
            if (session.isOpen()) {
    
    
                session.close();
            }
            webSockets.remove(this);
            sessionPool.remove(userId);
            log.info("【websocket消息】连接[错误]断开,总数为: {}, 错误:{}", webSockets.size(), t.getMessage());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    // 此为广播消息
    public void sendAllMessage(String message) {
    
    
        log.info("【websocket消息】广播消息:" + message);
        for (WebSocket webSocket : webSockets) {
    
    
            try {
    
    
                if (webSocket.session.isOpen()) {
    
    
                    webSocket.session.getAsyncRemote().sendText(message);
                }
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
    }

    // 此为单点消息
    public void sendOneMessage(String userId, String message) {
    
    
        Session session = sessionPool.get(userId);
        if (session != null && session.isOpen()) {
    
    
            try {
    
    
                log.info("【websocket消息】 单点消息:" + message);
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
    }

    // 此为单点消息(多人)
    public void sendMoreMessage(String[] userIds, String message) {
    
    
        for (String userId : userIds) {
    
    
            Session session = sessionPool.get(userId);
            if (session != null && session.isOpen()) {
    
    
                try {
    
    
                    log.info("【websocket消息】 单点消息:" + message);
                    session.getAsyncRemote().sendText(message);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
        }

    }

}

在RunApplication中加入:

package org.jeecg.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    
    
      /**
     * 会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
     * 要注意,如果使用独立的servlet容器,
     * 而不是直接使用springboot的内置容器,
     * 就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
     *  注入ServerEndpointExporter,
     * 	这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
    
    
      return new ServerEndpointExporter();
    }
    
}

可以编写一个test 测试连接

public class WebSocketTest {
    
    

    public static void main(String[] args) {
    
    
        try {
    
    

            // 创建WebSocket客户端
            MyWebSocketClient myClient = new MyWebSocketClient(new URI("ws://127.0.0.1:9091/web/websocket/123333"));
            // 与服务端建立连接
            myClient.connect();
            while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
    
    
                System.out.println("连接中。。。");
                Thread.sleep(1000);
            }
            // 往websocket服务端发送数据
            myClient.send("发送来自websocketClient 123333的消息");
            Thread.sleep(1000);
            // 关闭与服务端的连接
            // myClient.close();
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
        // write your code here

    }
}

实际开发使用:

package org.jeecg.modules.food.job;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.modules.food.entity.DailyMenu;
import org.jeecg.modules.food.mapper.DailyMenuMapper;
import org.jeecg.modules.message.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Auther: wsf
 * @Date: 2023/5/30 15:52
 * @Description:定时每天10、14点定时推送订餐消息
 */
@Component
public class DailyMenuTask {
    
    

    @Resource
    private WebSocket webSocket;

    @Autowired
    private DailyMenuMapper dailyMenuMapper;

    @Scheduled(cron = "0 0 10,14 * * ?")
    public void pushDailyMenu() {
    
    
        DailyMenu dailyMenu = new DailyMenu();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 1);
        Date time = cal.getTime();
        dailyMenu.setFoodDate(time);
        List<DailyMenu> dailyMenus = dailyMenuMapper.selectByDate(dailyMenu);
        if (CollectionUtils.isNotEmpty(dailyMenus)) {
    
    
            //创建业务消息信息
            JSONObject obj = new JSONObject();
            // 业务类型
            obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
            obj.put(WebsocketConst.MSG_ID, dailyMenus.get(0).getId());
            obj.put(WebsocketConst.MSG_TXT, "订餐发布");
            //全体发送
            webSocket.sendAllMessage(obj.toJSONString());
        }

    }


}

java 实现websocket的方式有很多种

  1. java 实现websocket一般两种方式,一种使用tomcat的websocket实现,比如上述使用这种方式无需别的任何配置,只需服务端一个处理类;
  2. 使用spring的websocket,spring与websocket整合需要spring 4.x,并且使用了socketjs,对不支持websocket的浏览器可以模拟websocket使用。实现WebSocketConfigurer接口的registerWebSocketHandlers方法加入自己WebSocketHandler接口的实现类。
  3. 使用spring stomp封装的方法,实现更简单,配置注入后通过@sendto就可向客户端发消息。
  4. 等等

猜你喜欢

转载自blog.csdn.net/rainAndyou/article/details/130678911