| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import java.io.ByteArrayOutputStream; |
| | | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Type; |
| | | import java.nio.ByteBuffer; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.locks.Lock; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.zip.Deflater; |
| | | import java.util.zip.DeflaterOutputStream; |
| | | |
| | | /** |
| | | * @ClassDescription: websocket服务端 |
| | |
| | | sessionLock.lock(); |
| | | try { |
| | | synchronized (session){ |
| | | session.getBasicRemote().sendText(message); |
| | | // 压缩消息 |
| | | byte[] compressedData = compress(message); |
| | | |
| | | // 发送压缩后的消息 |
| | | session.getBasicRemote().sendBinary(ByteBuffer.wrap(compressedData)); |
| | | // session.getBasicRemote().sendText(message); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("发送消息时出现异常: " + e.getMessage()); |
| | |
| | | } |
| | | } |
| | | |
| | | private byte[] compress(String data) throws IOException { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true); |
| | | try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater)) { |
| | | deflaterOutputStream.write(data.getBytes("UTF-8")); |
| | | } |
| | | deflater.end(); |
| | | return byteArrayOutputStream.toByteArray(); |
| | | } |
| | | |
| | | // |
| | | // public static Map<String, Object> jsonToMap(String json) { |
| | | // Gson gson = new Gson(); |