java 作为websocket客户端连接服务端

1. 作为客户端 连接服务端

  2.  发送心跳,断线重连

package com.api.cloudenergyweb_server.config;

import com.api.Engine.PushServer;
import com.api.cloudenergyweb_server.config.socket.MyWebSocketHandler;
import com.api.cloudenergyweb_server.utils.StringUtilsCustom;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.Map;

/**
 * 客户端,连接'数据层'服务端
 */
@Slf4j
public class WebSocketClientConfig {

    @Value("${cloud.db.server.appid}")
    private String appid;

    @Value("${cloud.db.server.websocket.url}")
    private String url;

    @Bean
    public WebSocketClient webSocketClient() {
        try {
            WebSocketClient webSocketClient = new WebSocketClient(new URI(url + appid)) {

                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    log.trace("[trace] 连接成功");
                }

                @Override
                public void onMessage(String message) {
                    try{
                        log.trace("[websocket] 收到消息={}",message);
                        if(StringUtilsCustom.isBlank(message) && !message.equals("first") && !message.equals("HeartBeat")){
                            try {
                                JSONObject json = JSONObject.fromObject(message);
                                PushServer.getInstance().pushMsg(json.getString("iot"), json.getJSONObject("msgData"), json.getString("msgKey"));
                                Map data = json.getJSONObject("msgData");
                                String equipment = data.get("equipment") + "";
                                if(StringUtilsCustom.isBlank(equipment)){
                                    MyWebSocketHandler.getInstance().sendRealtimeVarMessageByEquipment(json.getString("iot"), equipment, message);
                                }
                            }catch (Exception e){
                            }
                        }
                    }catch (Exception e){
                        log.error(e.getMessage(), e);
                    }
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    log.trace("[websocket] 退出连接");
                }

                @Override
                public void onError(Exception ex) {
                    log.trace("[websocket] 连接错误={}",ex.getMessage());
                }
            };
            webSocketClient.connect();
            new Thread(new Runnable() {
                public void run() {
                    System.out.println("Runnable running..");
                }
            }) {
                public void run() {
                    while (true){
                        try{
                            Thread.sleep(180000);
                            webSocketClient.send("HeartBeat");
                        }catch (Exception e){
                            webSocketClient.reconnect();
                        }
                    }
                }
            }.start();
            return webSocketClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
 
 

你可能感兴趣的:(java,开发语言,后端)