zoukankan      html  css  js  c++  java
  • 基于Java API for WebSocket (JSR-356)的标准websocket客户端

    maven依赖

    springboot

     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    import xxx.service.CacheService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.websocket.ClientEndpoint;
    import javax.websocket.ContainerProvider;
    import javax.websocket.DeploymentException;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.WebSocketContainer;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    /***
     * 查询有多少空闲的worker
     * @author 
     */
    @ClientEndpoint
    @Slf4j
    @Component
    public class StatusSocketClient {
    
        private Session session;
    
        @Value("${ws.asr.status}")
        String statUrl;
    
        @Autowired
        CacheService cacheService;
    
    
        WebSocketContainer container;
    
        @PostConstruct
        public void init() throws URISyntaxException, IOException, DeploymentException {
            container = ContainerProvider.getWebSocketContainer();
            container.connectToServer(this, new URI(statUrl));
        }
    
        @PreDestroy
        public void destroy() throws IOException {
    
            //session.getAsyncRemote().sendText("EOS");
            if(session!= null)
            {
                if(session.isOpen())
                {
                    session.close();
                }
            }
        }
    
        @OnOpen
        public void open(Session session){
            log.info("Connected to Real-Time ASR Server Status Socket...");
            this.session = session;
        }
    
        @OnMessage
        public void onMessage(String json){
            log.info("Get a status message : " + json);
            cacheService.setCacheValue("asr.server.status",json);
        }
    
        @OnClose
        public void onClose(){
            log.info("Websocket closed");
        }
    
    
        @OnError
        public void onError(Session session, Throwable t) {
            t.printStackTrace();
        }
    
        public void send(String message){
            this.session.getAsyncRemote().sendText(message);
        }
    
        public void close() throws IOException {
            if(this.session.isOpen()){
                this.session.close();
            }
        }
    }

    ws.asr.status:ws://172.16.1.238:8888/client/ws/status

    他会从这个地址接收服务器状态信息,并且存redis储缓存

  • 相关阅读:
    root登录出现“sorry, that didn't work please try again”
    【自适应辛普森】积分计算
    【CF1553F】Pairwise Modulo
    调和级数的复杂度
    CF 1600-2000 的思维题
    中超热身赛(2021湘潭全国邀请赛-重现)补题
    牛客2021年度训练联盟热身训练赛第一场(讲题)
    新知识-Queue_循环队列
    新知识-valueOf(Leetcode 1556_千位分隔符)
    新知识-位运算(Leetcode 217_存在重复元素)
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11935006.html
Copyright © 2011-2022 走看看