当前位置: 首页>>代码示例>>Java>>正文


Java MessageHandler类代码示例

本文整理汇总了Java中javax.websocket.MessageHandler的典型用法代码示例。如果您正苦于以下问题:Java MessageHandler类的具体用法?Java MessageHandler怎么用?Java MessageHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MessageHandler类属于javax.websocket包,在下文中一共展示了MessageHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: WebsocketClient

import javax.websocket.MessageHandler; //导入依赖的package包/类
public WebsocketClient(String uri, final String sessionID, MessageHandler.Whole<String> messageHandler)
    throws IOException {

// add session ID so the request gets through LAMS security
Builder configBuilder = ClientEndpointConfig.Builder.create();
configBuilder.configurator(new Configurator() {
    @Override
    public void beforeRequest(Map<String, List<String>> headers) {
	headers.put("Cookie", Arrays.asList("JSESSIONID=" + sessionID));
    }
});
ClientEndpointConfig clientConfig = configBuilder.build();
this.websocketEndpoint = new WebsocketEndpoint(messageHandler);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
try {
    container.connectToServer(websocketEndpoint, clientConfig, new URI(uri));
} catch (DeploymentException | URISyntaxException e) {
    throw new IOException("Error while connecting to websocket server", e);
}
   }
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:21,代码来源:WebsocketClient.java

示例2: sendMessageBinary

import javax.websocket.MessageHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void sendMessageBinary(ByteBuffer msg, boolean last)
        throws WsIOException {
    if (binaryMsgHandler instanceof WrappedMessageHandler) {
        long maxMessageSize =
                ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize();
        if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) {
            throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG,
                    sm.getString("wsFrame.messageTooBig",
                            Long.valueOf(msg.remaining()),
                            Long.valueOf(maxMessageSize))));
        }
    }
    try {
        if (binaryMsgHandler instanceof MessageHandler.Partial<?>) {
            ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last);
        } else {
            // Caller ensures last == true if this branch is used
            ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg);
        }
    } catch(Throwable t) {
        handleThrowableOnSend(t);
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:25,代码来源:WsFrameBase.java

示例3: onClose

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public final void onClose(Session session, CloseReason closeReason) {

    if (methodMapping.getOnClose() != null) {
        try {
            methodMapping.getOnClose().invoke(pojo,
                    methodMapping.getOnCloseArgs(pathParameters, session, closeReason));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("pojoEndpointBase.onCloseFail",
                    pojo.getClass().getName()), t);
        }
    }

    // Trigger the destroy method for any associated decoders
    Set<MessageHandler> messageHandlers = session.getMessageHandlers();
    for (MessageHandler messageHandler : messageHandlers) {
        if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
            ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
        }
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:23,代码来源:PojoEndpointBase.java

示例4: sendMessageText

import javax.websocket.MessageHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void sendMessageText(boolean last) throws WsIOException {
	if (textMsgHandler instanceof WrappedMessageHandler) {
		long maxMessageSize = ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize();
		if (maxMessageSize > -1 && messageBufferText.remaining() > maxMessageSize) {
			throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig",
					Long.valueOf(messageBufferText.remaining()), Long.valueOf(maxMessageSize))));
		}
	}

	try {
		if (textMsgHandler instanceof MessageHandler.Partial<?>) {
			((MessageHandler.Partial<String>) textMsgHandler).onMessage(messageBufferText.toString(), last);
		} else {
			// Caller ensures last == true if this branch is used
			((MessageHandler.Whole<String>) textMsgHandler).onMessage(messageBufferText.toString());
		}
	} catch (Throwable t) {
		handleThrowableOnSend(t);
	} finally {
		messageBufferText.clear();
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:24,代码来源:WsFrameBase.java

示例5: sendMessageBinary

import javax.websocket.MessageHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOException {
	if (binaryMsgHandler instanceof WrappedMessageHandler) {
		long maxMessageSize = ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize();
		if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) {
			throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig",
					Long.valueOf(msg.remaining()), Long.valueOf(maxMessageSize))));
		}
	}
	try {
		if (binaryMsgHandler instanceof MessageHandler.Partial<?>) {
			((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last);
		} else {
			// Caller ensures last == true if this branch is used
			((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg);
		}
	} catch (Throwable t) {
		handleThrowableOnSend(t);
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:21,代码来源:WsFrameBase.java

示例6: onClose

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public final void onClose(Session session, CloseReason closeReason) {

	if (methodMapping.getOnClose() != null) {
		try {
			methodMapping.getOnClose().invoke(pojo,
					methodMapping.getOnCloseArgs(pathParameters, session, closeReason));
		} catch (Throwable t) {
			ExceptionUtils.handleThrowable(t);
			log.error(sm.getString("pojoEndpointBase.onCloseFail", pojo.getClass().getName()), t);
		}
	}

	// Trigger the destroy method for any associated decoders
	Set<MessageHandler> messageHandlers = session.getMessageHandlers();
	for (MessageHandler messageHandler : messageHandlers) {
		if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
			((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
		}
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:22,代码来源:PojoEndpointBase.java

示例7: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
public void onOpen(final Session session, EndpointConfig endpointConfig) {
	session.getAsyncRemote().sendText(
			"Client Success!Your id is: " + session.getId());
	session.addMessageHandler(new MessageHandler.Whole<String>() {
		@Override
		public void onMessage(String message) {
			session.getAsyncRemote().sendObject(message, new SendHandler() {
				@Override
				public void onResult(SendResult result) {
					System.out.println(session.getId() + ":"
							+ result.isOK());
				}
			});
		}
	});
}
 
开发者ID:legend0702,项目名称:zhq,代码行数:17,代码来源:WebSocketServer.java

示例8: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public void onOpen(Session sn, EndpointConfig ec) {
  System.out.println("In DataMessageEndpoint#onOpen");
  this.session = sn;

  //TODO: Investgate why lambda version of the following didn't work:
  this.session.addMessageHandler(new MessageHandler.Whole<String>() {
    public void onMessage(String jsonReading) {
      try {
        ObjectMapper mapper = new ObjectMapper();
        Reading reading = mapper.readValue(jsonReading, Reading.class);
        System.out.println("New reading: " + reading.toString());
        Platform.runLater(() -> readingConsumer.setReading(reading));

      }
      catch (Exception e) {
        System.out.println("Exception: " + e.getLocalizedMessage());
      }
    }
  });
}
 
开发者ID:TRIPJavaFXClientBraintrust,项目名称:TRIPfx,代码行数:22,代码来源:DataMessageEndpoint.java

示例9: debugDumpSessionInfo

import javax.websocket.MessageHandler; //导入依赖的package包/类
private void debugDumpSessionInfo() {
    if (srrp.getSessions().size() == 0) {
        Log.log(Level.FINE,this, " No sessions known.");
    }
    for (Session s : srrp.getSessions()) {
        Log.log(Level.FINE,this, " Session: " + s.getId());
        Log.log(Level.FINE,this, "   handlers: " + s.getMessageHandlers().size());
        int mhc = 0;
        for (MessageHandler m : s.getMessageHandlers()) {
            if (m instanceof SessionMessageHandler) {
                SessionMessageHandler smh = (SessionMessageHandler) m;
                Log.log(Level.FINE,this, "    [" + mhc + "] SessionMessageHandler for session " + smh.session.getId()
                        + " linked to room " + smh.owner.room.getRoomId());
            } else {
                Log.log(Level.FINE,this, "    [" + mhc + "] unknown handler");
            }
            mhc++;
        }
    }
}
 
开发者ID:gameontext,项目名称:gameon-room,代码行数:21,代码来源:RoomWS.java

示例10: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public void onOpen(Session session, EndpointConfig config) {
  log.debug("Session {} open on channel", session.getId());

  session.addMessageHandler(new MessageHandler.Whole<byte[]>() {

    @Override
    public void onMessage(byte[] message) {
      try {
        WebSocketEndpoint.this.onMessage(message, session);
      } catch (Exception e) {
        log.error("Unexpected exception", e);
      }
    }
  });
}
 
开发者ID:mrstampy,项目名称:gameboot,代码行数:17,代码来源:WebSocketEndpoint.java

示例11: assertMessageReceived

import javax.websocket.MessageHandler; //导入依赖的package包/类
private void assertMessageReceived( String endpoint, String expectedMessage, String messageToSend ) throws Exception {
    final SettableFuture<String> futureMessage = SettableFuture.create();

    client.connectToServer( new Endpoint() {

        @Override
        public void onOpen( Session session, EndpointConfig config ) {
            clientSession = session;
            try {
                session.addMessageHandler( new MessageHandler.Whole<String>() {

                    @Override
                    public void onMessage( String message ) {
                        System.out.println( "Received message: " + message );
                        futureMessage.set( message );
                    }
                } );
                session.getBasicRemote().sendText( messageToSend );
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }, cec, new URI( "ws://localhost:8025/" + endpoint ) );

    assertEquals( expectedMessage, futureMessage.get( 2, TimeUnit.SECONDS ) );
}
 
开发者ID:renatoathaydes,项目名称:spark-ws,代码行数:27,代码来源:SparkWSTest.java

示例12: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public void onOpen(final Session session, EndpointConfig ec) {
    session.addMessageHandler(new MessageHandler.Whole<String>() {

        @Override
        public void onMessage(String text) {
            System.out.println("Received response in client from endpoint: " + text);
        }
    });
    System.out.println("Connected to endpoint: " + session.getBasicRemote());
    try {
        String name = "Duke";
        System.out.println("Sending message from client -> endpoint: " + name);
        session.getBasicRemote().sendText(name);
    } catch (IOException ex) {
        Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
开发者ID:ftomassetti,项目名称:JavaIncrementalParser,代码行数:19,代码来源:MyClient.java

示例13: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public void onOpen(final Session session, EndpointConfig ec) {
    session.addMessageHandler(new MessageHandler.Whole<String>() {

        @Override
        public void onMessage(String data) {
            System.out.println("Received (MyEndpointHandler) : " + data);

            session.getAsyncRemote().sendText(data, new SendHandler() {

                @Override
                public void onResult(SendResult sr) {
                    if (sr.isOK()) {
                        System.out.println("Message written to the socket (handler)");
                    } else {
                        System.out.println("Message NOT written to the socket (handler)");
                        sr.getException().printStackTrace();
                    }

                }
            });
        }
    });
}
 
开发者ID:ftomassetti,项目名称:JavaIncrementalParser,代码行数:25,代码来源:MyEndpointHandler.java

示例14: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
@Override
public void onOpen(Session session, EndpointConfig config) {
	final String sessionId = session.getId();
	log.info("established session with id: "+sessionId);
	
	//add text based message handler
	session.addMessageHandler(new MessageHandler.Whole<String>() {
		
		@Override
		public void onMessage(String msg) {
			log.info(sessionId+": text message: "+msg);
		}
	});
	
	//add binary based message handler
	session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

		@Override
		public void onMessage(ByteBuffer buffer) {
			log.info(sessionId+": binary message: "+new String(buffer.array()));
		}
	});
}
 
开发者ID:nickytd,项目名称:websocket-message-handlers-example,代码行数:24,代码来源:FullEchoEndpoint.java

示例15: onOpen

import javax.websocket.MessageHandler; //导入依赖的package包/类
/**
 * Callback hook for Connection open events.
 *
 * @param userSession the userSession which is opened.
 */
@Override
public void onOpen( Session userSession, EndpointConfig endpointConfig ) {
  this.userSession = userSession;
  this.userSession.setMaxTextMessageBufferSize( MAX_TXT_MSG_BUF_SIZE );
  this.userSession.setMaxBinaryMessageBufferSize( MAX_BIN_MSG_BUF_SIZE );

  userSession.addMessageHandler( new MessageHandler.Whole<Message>() {
    /**
     * Callback hook for Message Events. This method will be invoked when the server send a message.
     *
     * @param message The text message
     */
    @Override
    public void onMessage( Message message ) {
      try {
        messageEventService.fireEvent( message );
      } catch ( MessageEventFireEventException e ) {
        throw new RuntimeException( e );
      }
    }
  } );
}
 
开发者ID:pentaho,项目名称:pentaho-kettle,代码行数:28,代码来源:DaemonMessagesClientEndpoint.java


注:本文中的javax.websocket.MessageHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。