本文整理汇总了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);
}
}
示例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);
}
}
示例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();
}
}
}
示例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();
}
}
示例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);
}
}
示例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();
}
}
}
示例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());
}
});
}
});
}
示例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());
}
}
});
}
示例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++;
}
}
}
示例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);
}
}
});
}
示例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 ) );
}
示例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);
}
}
示例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();
}
}
});
}
});
}
示例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()));
}
});
}
示例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 );
}
}
} );
}