package com.changfa.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.websocket.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;

@Component
@ClientEndpoint
public class WebSocketClient {

    @Value("${websocket.server.url:127.0.0.1:8692/ws}")
    private String serverUrl;

    @Value("${websocket.server.user:system@1}")
    private String user;

    @Value("${websocket.server.isOpen:false}")
    private boolean isOpen;

    private Session session;

    @PostConstruct
    void init() {
        try {
            if(!isOpen){
                return;
            }
            // 本机地址
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            String wsUrl = "ws://" + serverUrl + "/" + user;
            URI uri = URI.create(wsUrl);
            session = container.connectToServer(WebSocketClient.class, uri);
        } catch (DeploymentException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开连接
     * @param session
     */
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("打开");
        this.session = session;
    }

    /**
     * 接收消息
     * @param text
     */
    @OnMessage
    public void onMessage(String text) {
        System.out.println(text);

    }

    /**
     * 异常处理
     * @param throwable
     */
    @OnError
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }

    /**
     * 关闭连接
     */
    @OnClose
    public void onClosing() throws IOException {
        System.out.println("关闭");
        session.close();
    }

    /**
     * 主动发送消息
     */
    public void send(String message) {
        try{
            this.session.getAsyncRemote().sendText(message);
        }catch (Exception e){

        }
    }
    public void close() throws IOException{
        if(this.session.isOpen()){
            this.session.close();
        }
    }


}
