1
zj
2024-07-18 ff2ca71784a9c603d84597d11944b4065c16feab
src/main/java/com/nq/ws/WebsocketRunClient.java
@@ -9,12 +9,22 @@
import com.nq.utils.ApplicationContextRegisterUtil;
import com.nq.utils.redis.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.context.ApplicationContext;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@@ -30,7 +40,7 @@
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()+":v1").getBytes());
    }
    @Override
@@ -41,8 +51,9 @@
            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
            StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
            liquidationService.RealTimeDataProcess(eStockType,stockDetailBean);
            send(s);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
@@ -61,4 +72,36 @@
    public void onError(Exception e) {
        log.info("websocket 错误");
    }
    private HttpClient httpClient;  //  声明一个成员变量
    private HttpPost httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification");  //  创建一个HttpPost对象
    @Override
    public void send(String message) {
        try {
            if (httpClient == null) {
                httpClient = HttpClients.createDefault();  //  使用单例模式创建HttpClient对象
            }
            //  准备  form-data  参数
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("message", message));
            //  设置编码格式为  UTF-8
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
            httpPost.setEntity(entity);  //  设置HttpPost对象的参数
            //  发送请求
            HttpResponse response = httpClient.execute(httpPost);
            //  处理响应
            int statusCode = response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}