webSocket 后台发送给前台消息,实现页面跳转消息提示

第一步:
//pom文件添加依赖,thymeleaf是模板,视情况是否添加

<!--websocket-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-websocket</artifactId>
			<version>4.3.8.RELEASE</version>
		</dependency>

 

第二步:

//启动类增加bean,和main方法同级

 /**springboot 自带tomcat 所以 打成war包之前需要把下面这几行代码注释掉   ,注释之后war包放到务器的tomcat下就没问题了
   * springboot内置tomcat的话,需要配一下这个。。如果没有这个对象,无法连接到websocket
   */
@Bean
public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
}
第三步:

//建websocket后台代码

package com.ts.websocket.util;


import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 */
@ServerEndpoint("/websocket/{userno}")
@Component
public class WebSocketUtil {

    private static final String loggerName = WebSocketUtil.class.getName();
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    private static ConcurrentHashMap<String, WebSocketUtil> webSocketSet = new ConcurrentHashMap<String, WebSocketUtil>();
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session webSocketSession;
    //当前发消息的人员编号
    private String userno = "";


    /**
     * 连接建立成功调用的方法
     *
     * @param webSocketSession 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(@PathParam(value = "userno") String param, Session webSocketSession, EndpointConfig config) {
        System.out.println(param);
        userno = param;//接收到发送消息的人员编号
        this.webSocketSession = webSocketSession;
        webSocketSet.put(param, this);//加入map中
        addOnlineCount();           //在线数加1
        System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
    }


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


    /**
     * 收到客户端消息后调用的方法
     * 也可以后台调用发送数据
     *  message 格式约定为JSON字符串。
     *  例:{"to":"All","time":"2020-09-09 16:22:42","message":"自定义消息","type":"text"}
     *      to:发送给的用户
     *      time:时间
     *      type:类型:text时就是字符串  url时就是跳转的地址 (和前端约定)
     *      message:消息
     *
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message) throws IOException {
        //约定message格式为此格式
        JSONObject jsonTo = JSONObject.parseObject(message);
//        String mes = (String) jsonTo.get("message");
        if (!jsonTo.get("to").equals("All")) {
            sendMessageTo(message, jsonTo.get("to").toString());
        } else {
            sendMessageAll(message);
        }
    }

    public void sendMessageTo(String message, String to) throws IOException {
        String now = getNowTime();
        if (webSocketSet.get(to) != null) {
//            webSocketSet.get(to).sendMessage("<br/>" + now + "用户" + userno + "发来消息:" + message);
            webSocketSet.get(to).sendMessage(message);
        } else {
            System.out.println("当前用户不在线");
        }
    }


    public void sendMessageAll(String message) throws IOException {
        String now = getNowTime();
        //遍历HashMap
        for (String key : webSocketSet.keySet()) {
            try {
                //判断接收用户是否是当前发消息的用户
                if (!userno.equals(key)) {
                    webSocketSet.get(key).sendMessage(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 获取当前时间
     *
     * @return
     */
    private String getNowTime() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }

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


    /**
     * getBasicRemote 同步发送,多条消息的话要等到上一条发完才可以发下一条消息
     * getAsyncRemote 异步发送
     *
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException {
        this.webSocketSession.getAsyncRemote().sendText(message);
    }


    public static synchronized int getOnlineCount() {
        return onlineCount;
    }


    public static synchronized void addOnlineCount() {
        WebSocketUtil.onlineCount++;
    }


    public static synchronized void subOnlineCount() {
        WebSocketUtil.onlineCount--;
    }


}

 //将message改造成自己需要的 MessageVO格式

package com.ts.websocket.vo;

/**
 *  
 *  webSocket 约定的 message值类型
 */
public class MessageVO {

    /**
     * 时间
     */
    private String time;
    /**
     * 发送人
     */
    private String from;
    /**
     * 接收人
     */
    private String to;
    /**
     * 类型:
     * text 是文本
     * url 是地址 (前端约定类型是url时候message值是地址,前端跳转指定地址)
     */
    private String type;
    /**
     * 消息
     */
    private String message;


    /*********************************get/set***********************************************/
    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

第四步:

//前台页面  下面这个是参考的页面 名称:copy1.html 访问:http://127.0.0.1:8081/项目地址/copy1.html  (有shiro拦截的记得关)

发送的数据格式是:{"to":"All","time":"2020-09-09 16:22:42","message":"自定义消息","type":"text"}   

JSON数据格式,因为上面后台写了处理,所以格式就定MessageVO 的格式

没打印在页面是应为群发的把自己屏蔽了,把 to 的值改为 1 就可以了 

此处的 1 代表的是用户注册的 websocket 的标识,是参数,后面自行替换成自己需要的参数就可以了。

<html>
<head>
    <meta charset="UTF-8"></meta>
    <title>springboot项目WebSocket测试demo</title>
</head>
<body>
<h3>springboot项目websocket测试demo</h3>
<h4>测试说明</h4>
<h5>文本框中数据数据,点击‘发送测试’,文本框中的数据会发送到后台websocket,后台接受到之后,会再推送数据到前端,展示在下方;点击关闭连接,可以关闭该websocket;可以跟踪代码,了解具体的流程;代码上有详细注解</h5>
<br />
<input id="text" type="text" />
<button onclick="send()">发送测试</button>
<hr />
<button onclick="clos()">关闭连接</button>
<hr />
<div id="message"></div>
<script>
    var websocket = null;
    if('WebSocket' in window){
        websocket = new WebSocket("ws://127.0.0.1:8081/项目标识/websocket/1");
    }else{
        alert("您的浏览器不支持websocket");
    }
    websocket.onerror = function(){
        setMessageInHtml("send error!");
    }
    websocket.onopen = function(){
        setMessageInHtml("connection success!")
    }
    websocket.onmessage  = function(event){
        setMessageInHtml(event.data);
    }
    websocket.onclose = function(){
        setMessageInHtml("closed websocket!")
    }
    window.onbeforeunload = function(){
        clos();
    }
    function setMessageInHtml(message){
        document.getElementById('message').innerHTML += message;
    }
    function clos(){
        websocket.close(3000,"强制关闭");
    }
    function send(){
        var msg = document.getElementById('text').value;
        websocket.send(msg);
    }
</script>
</body>
</html>

下面是对js返回数据格式(约定好的)的更改:

websocket.onmessage = function(e) {
                    var message = e.data;
                    //约定message格式
                    var mesMap = JSON.parse(message);
                    if ("url"===mesMap.type) {
                        window.open(mesMap.message, '_self');
                        // window.open(mesMap.message);
                        // window.location.href=message;
                    }else{
                        console.log(message);
                    }
                };

第五步:

//后台方法调用

调用的前提是你的websocket连接已经建立,否则发送不成功。

        try {
                WebSocketUtil ws = new WebSocketUtil();
                MessageVO messageVO = new MessageVO();
                messageVO.setTime("2020-09-09 18:00:00");
                messageVO.setFrom("系统");
                messageVO.setTo("1");//注册的用户是谁就填谁
                messageVO.setType("text");//类型url表示跳转页面,text表示打印。(前端约定好)
                messageVO.setMessage("测试message");
                ws.onMessage(JSON.toJSONString(messageVO));

            }catch (IOException e){
                System.out.println("webSocket出错:"+e);
            }

完毕:初步的就这样了,其他的优化再说吧

参考:

https://blog.csdn.net/qq_39478853/article/details/79413453
https://blog.csdn.net/qq_41700030/article/details/100777937
https://blog.csdn.net/qq_27409289/article/details/81814272

 

----------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------

简单的优化

替换第三步中的 WebSocketUtil 类。

onMessage 方法是可以有返回值的,这样就可以在后台调用的时候返回知道发送给指定的用户是成功还是失败了。

package com.ts.websocket.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ts.websocket.vo.WebSocketResult;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 */
@ServerEndpoint("/websocket/{userno}")
@Component
public class WebSocketUtil {

    private static final String loggerName = WebSocketUtil.class.getName();
    private static final String sendAll = "All";
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    private static ConcurrentHashMap<String, WebSocketUtil> webSocketSet = new ConcurrentHashMap<String, WebSocketUtil>();
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session webSocketSession;
    //当前发消息的人员编号
    private String userno = "";


    /**
     * 连接建立成功调用的方法
     *
     * @param webSocketSession 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(@PathParam(value = "userno") String param, Session webSocketSession, EndpointConfig config) {
        System.out.println(param);
        userno = param;//接收到发送消息的人员编号
        this.webSocketSession = webSocketSession;
        webSocketSet.put(param, this);//加入map中
        addOnlineCount();           //在线数加1
        System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
    }


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


    /**
     * 收到客户端消息后调用的方法
     * 也可以后台调用发送数据
     * message 格式约定为JSON字符串。
     * 例:{"to":"All","time":"2020-09-09 16:22:42","message":"自定义消息","type":"text"}
     * to:发送给的用户
     * time:时间
     * type:类型:text时就是字符串  url时就是跳转的地址 (和前端约定)
     * message:消息
     *
     * @param message 客户端发送过来的消息
     * @return JSON格式的String字符串,约定为WebSocketResult格式
     */
    @OnMessage
    public String onMessage(String message) {
        //约定message格式为此格式
        JSONObject jsonTo = JSONObject.parseObject(message);
        if (!jsonTo.get("to").equals(sendAll)) {
            return sendMessageTo(message, jsonTo.get("to").toString());
        } else {
            return sendMessageAll(message);
        }
    }

    public String sendMessageTo(String message, String to) {
        WebSocketResult webSocketResult = new WebSocketResult(WebSocketResult.success, "");
        try {
            if (webSocketSet.get(to) != null) {
                webSocketSet.get(to).sendMessage(message);
            } else {
                webSocketResult = new WebSocketResult(WebSocketResult.notOnline, "当前用户不在线");
            }
        } catch (Exception e) {
            webSocketResult = new WebSocketResult("error", e.getMessage());
        }

        return JSON.toJSONString(webSocketResult);
    }


    public String sendMessageAll(String message) {
        WebSocketResult webSocketResult = new WebSocketResult(WebSocketResult.success, "");
        String now = getNowTime();
        //遍历HashMap
        for (String key : webSocketSet.keySet()) {
            try {
                //判断接收用户是否是当前发消息的用户
                if (!userno.equals(key)) {
                    webSocketSet.get(key).sendMessage(message);
                }
            } catch (Exception e) {
                webSocketResult = new WebSocketResult(WebSocketResult.error, e.getMessage());
            }
        }
        return JSON.toJSONString(webSocketResult);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    private String getNowTime() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }

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


    /**
     * getBasicRemote 同步发送,多条消息的话要等到上一条发完才可以发下一条消息
     * getAsyncRemote 异步发送
     *
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException {
        this.webSocketSession.getAsyncRemote().sendText(message);
    }


    public static synchronized int getOnlineCount() {
        return onlineCount;
    }


    public static synchronized void addOnlineCount() {
        WebSocketUtil.onlineCount++;
    }


    public static synchronized void subOnlineCount() {
        WebSocketUtil.onlineCount--;
    }


}

增加一个返回类型类:

package com.ts.websocket.vo;

public class WebSocketResult {

    //发送成功
    public static final String success = "success";
    //发送失败
    public static final String error = "error";
    //发送的用户不存在
    public static final String notOnline = "notOnline";

    /**
     * 状态:success 成功
     * notOnline 不在线(给单个用户发)
     * error 失败
     */
    private String state;
    /**
     * 提示信息
     */
    private String message;


    public WebSocketResult() {
        super();
    }

    public WebSocketResult(String state, String message) {
        this.state = state;
        this.message = message;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

此时后台调用的方法:

    @ResponseBody
    @PostMapping(value = "/dw/redirectUrl")
    @ApiOperation(value = "测试接口")
    public SysResult testDW2(@RequestBody DwParamVO dwParamVO) {
        SysResult sysResult = new SysResult();//自己接口返回格式
        try {
            WebSocketUtil ws = new WebSocketUtil();
            MessageVO messageVO = new MessageVO();
            messageVO.setFrom("系统");
            messageVO.setTo(dwParamVO.getIp());
            messageVO.setType("url");
            messageVO.setMessage(dwParamVO.getUrl());
            //发送消息
            String webSocketResult = ws.onMessage(JSON.toJSONString(messageVO));
            //对发送消息格式进行处理
            JSONObject jsonObject = JSON.parseObject(webSocketResult);
            //发送消息的返回,给自己的统一返回中做标识,以便处理
            if (WebSocketResult.success.equals(jsonObject.get("state"))) {
                sysResult.setObjOther(WebSocketResult.success);
            } else if (WebSocketResult.notOnline.equals(jsonObject.get("state"))) {
                sysResult.setObjOther(WebSocketResult.notOnline);
            } else {
                sysResult.setObjOther(WebSocketResult.error);
            }
        } catch (Exception e) {
            sysResult.setObjOther(WebSocketResult.error);
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的。 在 Java 中实现 WebSocket 定时发送消息,你需要用到一个定时任务的库,比如说 `java.util.Timer` 或者 `java.util.concurrent.ScheduledThreadPoolExecutor`。这些库都可以实现定时执行某个任务的功能。 下面是一个简单的例子,使用 `java.util.Timer` 实现 WebSocket 定时发送消息: ``` import java.util.Timer; import java.util.TimerTask; import javax.websocket.Session; public class MyWebSocket { // 定时任务 private static Timer timer = new Timer(); // WebSocket 会话 private Session session; public MyWebSocket(Session session) { this.session = session; } public void startTimer() { // 定义定时任务 TimerTask task = new TimerTask() { @Override public void run() { try { // 发送消息 session.getBasicRemote().sendText("定时发送消息"); } catch (Exception e) { // 异常处理 } } }; // 开始定时任务,每隔 5 秒执行一次 timer.schedule(task, 0, 5000); } public void stopTimer() { // 停止定时任务 timer.cancel(); } } ``` 这样,你就可以在你的 WebSocket 服务端代码中,调用 `startTimer` 方法来启动定时任务,调用 `stopTimer` 方法来停止定时任务。 希望这能帮到你。 ### 回答2: 要实现 Java 中的 WebSocket 定时发送消息,可以使用以下步骤: 1. 首先,需要导入 Java 的 WebSocket 相关的库。可以使用 Java EE 提供的 javax.websocket 包。 2. 创建一个 WebSocket 的类,该类需要继承 javax.websocket.Endpoint 类。在该类中,需要实现 onOpen、onClose、onMessage 和 onError 方法。 3. 在 onOpen 方法中,可以进行一些初始化操作,比如建立数据库连接等。 4. 在 onMessage 方法中,可以处理接收到的客户端消息。 5. 在 onClose 方法中,可以进行资源释放操作,比如关闭数据库连接。 6. 在 onError 方法中,可以处理发生的错误,比如日志记录等。 7. 在类中添加一个定时任务,使用 java.util.Timer 或者 java.util.concurrent.ScheduledExecutorService 类来执行定时任务。 8. 在定时任务中,使用 javax.websocket.Session 的 getBasicRemote 方法获取到 BasicRemote 对象,然后使用 BasicRemote 对象的 sendText 方法发送消息给客户端。 9. 使用 javax.websocket.server.ServerEndpoint 注解来指定 WebSocket 的路径和配置。 10. 在应用程序的入口类中,使用 javax.websocket.server.ServerContainer 的 addEndpoint 方法将创建的 WebSocket 类添加到容器中。 11. 部署应用程序,并启动服务器。 通过以上步骤,就可以实现 Java 中的 WebSocket 定时发送消息了。每当定时任务触发时,就可以向客户端发送指定的消息。 ### 回答3: 在Java中实现WebSocket定时发送消息的步骤如下: 1. 导入相关的包: import javax.websocket.ContainerProvider; import javax.websocket.DeploymentException; import javax.websocket.Session; import javax.websocket.WebSocketContainer; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; 2. 创建WebSocketClient类,实现Endpoint接口,并重写onOpen、onClose和onError方法: public class WebSocketClient extends Endpoint { private Session session; @Override public void onOpen(Session session, EndpointConfig config) { this.session = session; } @Override public void onClose(Session session, CloseReason closeReason) { // 处理关闭连接的逻辑 } @Override public void onError(Session session, Throwable throwable) { // 处理错误的逻辑 } public void sendMessage(String message) throws IOException { session.getBasicRemote().sendText(message); } } 3. 创建WebSocketServer类,用于启动WebSocket客户端并定时发送消息: public class WebSocketServer { public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, DeploymentException { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); Session session = container.connectToServer(WebSocketClient.class, new URI("wss://example.com")); // 修改为实际的WebSocket服务器地址 // 每隔1秒发送一次消息 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { try { ((WebSocketClient) session).sendMessage("Hello, WebSocket!"); // 修改为实际需要发送消息 } catch (IOException e) { e.printStackTrace(); } } }, 0, 1000); // 持续运行 Thread.sleep(Long.MAX_VALUE); } } 以上代码假设WebSocket服务器的地址为"wss://example.com",每隔1秒发送一次消息"Hello, WebSocket!"。可以根据实际需求修改相应的地址和消息内容。 请注意,WebSocket是一种双向通信协议,需要安装WebSocket服务器并编写相应的服务器端代码来接收和处理消息。以上代码只是实现了客户端的发送功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值