List of usage examples for org.springframework.web.socket BinaryMessage BinaryMessage
public BinaryMessage(byte[] payload)
From source file:samples.websocket.behavoir.RandomByteStreamWebSocketHandler.java
@Override public void afterConnectionEstablished(final WebSocketSession session) throws Exception { final byte[] payload = new byte[4]; Runnable sender = new Runnable() { @Override/*from ww w . j a v a 2 s . c o m*/ public void run() { while (true) try { new Random().nextBytes(payload); session.sendMessage(new BinaryMessage(payload)); Thread.sleep(10); } catch (Exception e) { return; } } }; new Thread(sender).run(); }
From source file:ch.rasc.s4ws.drawboard.DrawboardWebSocketHandler.java
@SuppressWarnings("resource") @Selector("sendBinary") public void consumeSendBinary(Event<ByteBuffer> event) { String receiver = event.getHeaders().get("sessionId"); BinaryMessage binMsg = new BinaryMessage(event.getData()); if (receiver != null) { WebSocketSession session = this.sessions.get(receiver); if (session != null) { try { session.sendMessage(binMsg); } catch (IOException e) { log.error("sendMessage", e); }/*from w w w. jav a 2s . c om*/ } } else { String excludeId = event.getHeaders().get("excludeId"); for (WebSocketSession session : this.sessions.values()) { if (!session.getId().equals(excludeId)) { try { session.sendMessage(binMsg); } catch (IOException e) { log.error("sendMessage", e); } } } } }
From source file:com.github.mrstampy.gameboot.otp.websocket.OtpEncryptedWebSocketHandler.java
/** * Process for binary./*w w w . j av a 2 s .co m*/ * * @param session * the session * @param msg * the msg * @throws Exception * the exception */ protected void processForBinary(WebSocketSession session, byte[] msg) throws Exception { OtpKeyRequest message = converter.fromJson(msg); if (!validateChannel(session, message)) return; Response r = processor.process(message); if (r == null || !r.isSuccess()) { log.error("Unexpected response {}, disconnecting {}", r, session); session.close(); return; } BinaryMessage bm = new BinaryMessage(converter.toJsonArray(r)); session.sendMessage(bm); log.debug("Successful send of {} to {}", message.getType(), session.getRemoteAddress()); Thread.sleep(50); session.close(CloseStatus.NORMAL); }
From source file:com.github.mrstampy.gameboot.websocket.WebSocketSessionRegistry.java
private void sendBinary(String groupName, WebSocketSession wss, byte[] message) { BinaryMessage bm = new BinaryMessage(message); try {// ww w . j a va 2s . c o m wss.sendMessage(bm); log.debug("Sent message to web socket session {} in group {}", wss.getId(), groupName); } catch (IOException e) { log.error("Unexpected exception sending message to web socket session {}", wss.getId(), e); } }
From source file:com.github.mrstampy.gameboot.websocket.AbstractWebSocketProcessor.java
/** * Creates the message./* w ww.j a v a 2s . co m*/ * * @param session * the session * @param msg * the msg * @return the web socket message * @throws Exception * the exception */ protected WebSocketMessage<?> createMessage(WebSocketSession session, Object msg) throws Exception { if (msg instanceof byte[]) return new BinaryMessage((byte[]) msg); if (msg instanceof String) return new TextMessage(((String) msg).getBytes()); throw new IllegalArgumentException("Can only send strings or byte arrays: " + msg.getClass()); }
From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java
private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload) { StompCommand command = stompAccessor.getCommand(); try {//from w w w . j a v a 2 s .co m byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), payload); boolean useBinary = (payload.length > 0 && !(session instanceof SockJsSession) && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType())); if (useBinary) { session.sendMessage(new BinaryMessage(bytes)); } else { session.sendMessage(new TextMessage(bytes)); } } catch (SessionLimitExceededException ex) { // Bad session, just get out throw ex; } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) if (logger.isDebugEnabled()) { logger.debug("Failed to send WebSocket message to client in session " + session.getId(), ex); } command = StompCommand.ERROR; } finally { if (StompCommand.ERROR.equals(command)) { try { session.close(CloseStatus.PROTOCOL_ERROR); } catch (IOException ex) { // Ignore } } } }