package *;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
@ServerEndpoint("/threed_model/ws/warehouse/moving") // 此注解相当于设置访问URL
public class WarehouseWs {
private static CopyOnWriteArraySet<WarehouseWs> webSockets = new CopyOnWriteArraySet<>();
private Session session;
@OnOpen
public void onOpen(Session session) {
try {
log.info("【websocket消息】有新的连接");
this.session = session;
webSockets.add(this);
} catch (Exception e) {
}
}
@OnClose
public void onClose() {
try {
log.info("【websocket消息】连接断开");
webSockets.remove(this);
} catch (Exception e) {
}
}
@OnMessage
public void onMessage(String message) {
log.info(message);
}
public static void pushMessage(String message) {
try {
webSockets.forEach(ws -> ws.session.getAsyncRemote().sendText(message));
} catch (Exception e) {
e.printStackTrace();
}
}
}