whyclxw
2022-01-07 dca5fa1b0367ccbc052ef953db6adb5c2e2bc64c
src/main/java/com/whyc/webSocket/DeviceWorkStateSocket.java
@@ -4,13 +4,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.EncodeException;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.security.Principal;
@ServerEndpoint(value = "/deviceWorkState/{deviceId}/{workState}/{uId}",encoders = WebSocketEncoder.class)
@Component
@@ -29,7 +26,7 @@
    @OnOpen
    public void onOpen(Session session, @PathParam("deviceId") Integer deviceId, @PathParam("workState") Integer workState, @PathParam("uId") Long uId){
        thread = new Thread("Thread_batteryData") {
        thread = new Thread("Thread_DeviceWorkState") {
            public void run() {
                while (!thread.isInterrupted()) {
                    try {
@@ -46,4 +43,18 @@
        thread.start();
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}