本文整理汇总了Java中org.springframework.web.socket.WebSocketMessage类的典型用法代码示例。如果您正苦于以下问题:Java WebSocketMessage类的具体用法?Java WebSocketMessage怎么用?Java WebSocketMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketMessage类属于org.springframework.web.socket包,在下文中一共展示了WebSocketMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
WebSocketClient client = new StandardWebSocketClient();
String url = "ws://localhost:8081/socket";
ListenableFuture<WebSocketSession> future = client.doHandshake(new AbstractWebSocketHandler() {
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
System.out.println(message.getPayload());
}
}, url);
WebSocketSession socketSession = future.get();
socketSession.sendMessage(new TextMessage("{\"command\":\"test\",\"parameters\":{\"type\":\"conn\"}}"));
System.in.read();
}
示例2: sendMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
public void sendMessage(WebSocketMessage<?> message) throws IOException {
if (isDisabled()) {
return;
}
this.buffer.add(message);
this.bufferSize.addAndGet(message.getPayloadLength());
do {
if (!tryFlushMessageBuffer()) {
if (logger.isTraceEnabled()) {
logger.trace("Another send already in progress, session id '" +
getId() + "'" + ", in-progress send time " + getTimeSinceSendStarted() +
" (ms)" + ", buffer size " + this.bufferSize + " bytes");
}
checkSessionLimits();
break;
}
}
while (!this.buffer.isEmpty() && !isDisabled());
}
示例3: tryFlushMessageBuffer
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
private boolean tryFlushMessageBuffer() throws IOException {
if (this.flushLock.tryLock()) {
try {
while (true) {
WebSocketMessage<?> messageToSend = this.buffer.poll();
if (messageToSend == null || isDisabled()) {
break;
}
this.bufferSize.addAndGet(messageToSend.getPayloadLength() * -1);
this.sendStartTime = System.currentTimeMillis();
getDelegate().sendMessage(messageToSend);
this.sendStartTime = 0;
}
}
finally {
this.sendStartTime = 0;
flushLock.unlock();
}
return true;
}
return false;
}
示例4: sendMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
checkNativeSessionInitialized();
Assert.isTrue(isOpen(), "Cannot send message after connection closed.");
if (logger.isTraceEnabled()) {
logger.trace("Sending " + message + ", " + this);
}
if (message instanceof TextMessage) {
sendTextMessage((TextMessage) message);
}
else if (message instanceof BinaryMessage) {
sendBinaryMessage((BinaryMessage) message);
}
else if (message instanceof PingMessage) {
sendPingMessage((PingMessage) message);
}
else if (message instanceof PongMessage) {
sendPongMessage((PongMessage) message);
}
else {
throw new IllegalStateException("Unexpected WebSocketMessage type: " + message);
}
}
示例5: decode
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
public List<Message<byte[]>> decode(WebSocketMessage<?> webSocketMessage) {
List<Message<byte[]>> result = Collections.<Message<byte[]>>emptyList();
ByteBuffer byteBuffer;
if (webSocketMessage instanceof TextMessage) {
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes());
}
else if (webSocketMessage instanceof BinaryMessage) {
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload();
}
else {
return result;
}
result = this.bufferingDecoder.decode(byteBuffer);
if (result.isEmpty()) {
if (logger.isTraceEnabled()) {
logger.trace("Incomplete STOMP frame content received, bufferSize=" +
this.bufferingDecoder.getBufferSize() + ", bufferSizeLimit=" +
this.bufferingDecoder.getBufferSizeLimit() + ".");
}
}
return result;
}
示例6: handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Test
public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException {
SockJsSession sockJsSession = Mockito.mock(SockJsSession.class);
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
accessor.setHeartbeat(0, 10);
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
this.protocolHandler.handleMessageToClient(sockJsSession, message);
verify(sockJsSession).getPrincipal();
verify(sockJsSession).disableHeartbeat();
verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
verifyNoMoreInteractions(sockJsSession);
sockJsSession = Mockito.mock(SockJsSession.class);
accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
accessor.setHeartbeat(0, 0);
message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
this.protocolHandler.handleMessageToClient(sockJsSession, message);
verify(sockJsSession).getPrincipal();
verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
verifyNoMoreInteractions(sockJsSession);
}
示例7: handleMessageToClientWithUserDestination
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Test
public void handleMessageToClientWithUserDestination() {
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
headers.setMessageId("mess0");
headers.setSubscriptionId("sub0");
headers.setDestination("/queue/foo-user123");
headers.setNativeHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo");
Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
this.protocolHandler.handleMessageToClient(this.session, message);
assertEquals(1, this.session.getSentMessages().size());
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertTrue(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n"));
assertFalse(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION));
}
示例8: getSession
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
private WebSocketSession getSession(final String id) {
messages.put(id, new LinkedList<WebSocketMessage<?>>());
WebSocketSession session = mock(WebSocketSession.class);
try {
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
messages.get(id).offer((WebSocketMessage<?>) invocation.getArguments()[0]);
return null;
}
}).when(session).sendMessage(Mockito.<WebSocketMessage<?>>any());
} catch (IOException e) {
throw new IllegalStateException(e);
}
when(session.getId()).thenReturn(id);
return session;
}
示例9: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (!(message instanceof TextMessage)) {
throw new IllegalStateException("Unexpected WebSocket message type: " + message);
}
String messageText = ((TextMessage)message).getPayload();
SSHSession sshSession = sshSessionManager.sessionsByWebsocketID.get(session.getId());
if (sshSession == null) {
log.info("linking {}:{}", session.getId(), messageText);
// TODO is there a better way to do this?
// Can the client send the websocket session id and username in a REST call to link them up?
sshSession = sshSessionManager.sessionsByUsername.get(messageText);
sshSession.setWebSocketSession(session);
sshSessionManager.sessionsByWebsocketID.put(session.getId(), sshSession);
} else {
log.debug("message in {}:{}", session.getId(), messageText);
sshSession.getSSHOutput().write((messageText + '\n').getBytes());
sshSession.getSSHOutput().flush();
}
// if we receive a valid logout command, then close the websocket session.
// the system will logout and tidy itself up...
if (logoutCommands.contains(messageText.trim().toLowerCase())) {
log.info("valid logout command received: {}", messageText);
session.close();
}
}
示例10: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (message instanceof TextMessage) {
handleTextMessage(session, (TextMessage) message);
} else if (message instanceof PongMessage) {
handlePongMessage(session);
} else if (message instanceof BinaryMessage) {
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported"));
} else {
throw new IllegalStateException("Unexpected WebSocket message type: " + message);
}
}
示例11: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (message instanceof TextMessage) {
handleTextMessage(session, (TextMessage) message);
}
else if (message instanceof BinaryMessage) {
handleBinaryMessage(session, (BinaryMessage) message);
}
else if (message instanceof PongMessage) {
handlePongMessage(session, (PongMessage) message);
}
else {
throw new IllegalStateException("Unexpected WebSocket message type: " + message);
}
}
示例12: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("Handling " + message + " in " + session);
}
super.handleMessage(session, message);
}
示例13: handleMessageFromClient
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
private void handleMessageFromClient(WebSocketSession session,
WebSocketMessage<?> message) throws Exception {
boolean handled = false;
WebSocketMessageAccessor accessor = WebSocketMessageAccessor.create(message);
if (StompCommand.SEND.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
sendMessageToProxiedTarget(session, accessor);
}
if (StompCommand.SUBSCRIBE.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
subscribeToProxiedTarget(session, accessor);
}
if (StompCommand.UNSUBSCRIBE.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
unsubscribeFromProxiedTarget(session, accessor);
}
if (StompCommand.CONNECT.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
connectToProxiedTarget(session);
}
if (!handled) {
if (logger.isDebugEnabled()) {
logger.debug("STOMP COMMAND " + accessor.getCommand()
+ " was not explicitly handled");
}
}
}
示例14: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) {
try {
getDelegate().handleMessage(session, message);
}
catch (Throwable ex) {
tryCloseWithError(session, ex, logger);
}
}
示例15: handleMessage
import org.springframework.web.socket.WebSocketMessage; //导入依赖的package包/类
/**
* Handle an inbound message from a WebSocket client.
*/
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
WebSocketSessionHolder holder = this.sessions.get(session.getId());
if (holder != null) {
session = holder.getSession();
}
SubProtocolHandler protocolHandler = findProtocolHandler(session);
protocolHandler.handleMessageFromClient(session, message, this.clientInboundChannel);
if (holder != null) {
holder.setHasHandledMessages();
}
checkSessions();
}