前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot+jsp WebSocketServer 消息提示音webSocketServer:WebSocketConfigjsp

springboot+jsp WebSocketServer 消息提示音webSocketServer:WebSocketConfigjsp

作者头像
用户5899361
发布2020-12-07 15:26:00
1.1K0
发布2020-12-07 15:26:00
举报
文章被收录于专栏:学习java的小白

webSocketServer:

代码语言:javascript
复制
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

/**
 * WebSocketServer服务器端
 *
 * @author han
 *
 */
@Component
@ServerEndpoint("/WebSocketServer")
public class WebSocketServer {
    // concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象,若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    private static CopyOnWriteArraySet<WebSocketServer> webSockets = new CopyOnWriteArraySet<WebSocketServer>();
    private Session session;

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        webSockets.add(this);

    }

    @OnClose
    public void onClose() {
        webSockets.remove(this);

          }

    @OnMessage
    public void onMessage(String message) {
    }

    /**
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
    }

    /**
     * 服务器主动推送消息
     */
    public void sendMessage(String message) {
        try {
            for (WebSocketServer webSocket : webSockets) {
                webSocket.session.getBasicRemote().sendText(message);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}

WebSocketConfig

代码语言:javascript
复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;


@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    /**
     * 注册webSocket端点
     */
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 添加一个/WebSocketServer端点,客户端就可以通过这个端点来进行连接;withSockJS作用是添加SockJS支持
        registry.addEndpoint("/WebSocketServer").setAllowedOrigins("*").withSockJS();
    }

}

jsp

代码语言:javascript
复制
<html>
<body>
<audio src="${path}/webapp/music/msg.mp3" id="myaudio" controls="controls" loop="false" hidden="true"></audio>
<script>
      
  var websocket=null;
        if('WebSocket' in  window){
            websocket=new WebSocket('ws://localhost:8080/WebSocketServer');
        }else{
            layer.msg('该浏览器不支持websocket',{icon:2,time:1000});
        }
        websocket.onopen=function(event){
            console.info('建立连接');
        }
        websocket.onclose=function(event){
            console.info('关闭连接');
        }
        websocket.onmessage=function(event){
            if(event.data!=null){
                document.getElementById('myaudio').play();
                layer.confirm("您有新的消息请注意查收!", {
                    btn: ['确定','取消']
                }, function(){
                    closePlay();

                },function(){
                    closePlay();
                });
            }
            //弹框提醒,播放音乐
        }
        websocket.onerror=function(){
            console.info('websocket通讯发生错误!');
        }
        websocket.onbeforeunload=function(){
            websocket.close();
        }

        function autoPlay() {
            var myAuto = document.getElementById('myaudio');
            myAuto.play();
        }
        function closePlay() {
            var myAuto = document.getElementById('myaudio');
            myAuto.pause();
            myAuto.load();
        }
</script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • webSocketServer:
  • WebSocketConfig
  • jsp
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档