本文整理匯總了Java中javax.websocket.EncodeException類的典型用法代碼示例。如果您正苦於以下問題:Java EncodeException類的具體用法?Java EncodeException怎麽用?Java EncodeException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
EncodeException類屬於javax.websocket包,在下文中一共展示了EncodeException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import javax.websocket.EncodeException; //導入依賴的package包/類
public String encode(UserVisitBean userDiagramData) throws EncodeException {
StringWriter writer = new StringWriter();
//Makes use of the JSON Streaming API to build the JSON string.
Json.createGenerator(writer)
.writeStartObject()
.write("time", String.valueOf(userDiagramData.getTime()))
.write("totalFlow", userDiagramData.getTotalFlow())
.write("checkInFlow", userDiagramData.getCheckInFlow())
.write("checkInRatio", userDiagramData.getCheckInRate())
.write("deepVisitRatio", userDiagramData.getDeepVisitRate())
.write("jumpRatio", userDiagramData.getShallowVisitRate())
.writeEnd()
.flush();
//System.out.println(writer.toString());
return writer.toString();
}
示例2: processResult
import javax.websocket.EncodeException; //導入依賴的package包/類
protected final void processResult(Object result) {
if (result == null) {
return;
}
RemoteEndpoint.Basic remoteEndpoint = session.getBasicRemote();
try {
if (result instanceof String) {
remoteEndpoint.sendText((String) result);
} else if (result instanceof ByteBuffer) {
remoteEndpoint.sendBinary((ByteBuffer) result);
} else if (result instanceof byte[]) {
remoteEndpoint.sendBinary(ByteBuffer.wrap((byte[]) result));
} else {
remoteEndpoint.sendObject(result);
}
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
} catch (EncodeException ee) {
throw new IllegalStateException(ee);
}
}
示例3: handleSendFailureWithEncode
import javax.websocket.EncodeException; //導入依賴的package包/類
private void handleSendFailureWithEncode(Throwable t) throws IOException, EncodeException {
// First, unwrap any execution exception
if (t instanceof ExecutionException) {
t = t.getCause();
}
// Close the session
wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()),
new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()));
// Rethrow the exception
if (t instanceof EncodeException) {
throw (EncodeException) t;
}
if (t instanceof IOException) {
throw (IOException) t;
}
throw new IOException(t);
}
示例4: processResult
import javax.websocket.EncodeException; //導入依賴的package包/類
protected final void processResult(Object result) {
if (result == null) {
return;
}
RemoteEndpoint.Basic remoteEndpoint = session.getBasicRemote();
try {
if (result instanceof String) {
remoteEndpoint.sendText((String) result);
} else if (result instanceof ByteBuffer) {
remoteEndpoint.sendBinary((ByteBuffer) result);
} else if (result instanceof byte[]) {
remoteEndpoint.sendBinary(ByteBuffer.wrap((byte[]) result));
} else {
remoteEndpoint.sendObject(result);
}
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
} catch (EncodeException ee) {
throw new IllegalStateException(ee);
}
}
示例5: handleSendFailureWithEncode
import javax.websocket.EncodeException; //導入依賴的package包/類
private void handleSendFailureWithEncode(Throwable t) throws IOException, EncodeException {
// First, unwrap any execution exception
if (t instanceof ExecutionException) {
t = t.getCause();
}
// Close the session
wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()),
new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()));
// Rethrow the exception
if (t instanceof EncodeException) {
throw (EncodeException) t;
}
if (t instanceof IOException) {
throw (IOException) t;
}
throw new IOException(t);
}
示例6: connect
import javax.websocket.EncodeException; //導入依賴的package包/類
public void connect(long timeout, TimeUnit unit) throws DeploymentException, IOException, EncodeException, InterruptedException, ExecutionException, TimeoutException {
if (getState() != State.DISCONNECTED) {
throw new IllegalStateException("Connection open, or in progress!");
}
this.session = ContainerProvider.getWebSocketContainer().connectToServer(this, this.uri);
this.state = State.CONNECTING;
final Frame connectFrame = Frame.connect(this.uri.getHost(), "1.2").heartbeat(5_000, 5_000).build();
sendToClient(connectFrame);
this.connectFuture = new CompletableFuture<>();
final Frame connectedFrame = this.connectFuture.get(timeout, unit);
this.connectFuture = null;
final long readDelay = Math.max(connectedFrame.heartBeat().get().x, connectFrame.heartBeat().get().y);
final long writeDelay = Math.max(connectFrame.heartBeat().get().x, connectedFrame.heartBeat().get().y);
this.heartBeatMonitor.start(readDelay, writeDelay);
}
示例7: fromBroker
import javax.websocket.EncodeException; //導入依賴的package包/類
/**
*
* @param msg
*/
public void fromBroker(@Observes @FromBroker Message msg) {
final String sessionId = msg.sessionId();
final Frame frame = msg.frame();
this.log.debug("Sending message to client. [sessionId={},command={}]",
sessionId, frame.command() != null ? frame.command() : "HEARTBEAT");
Optional<Session> session = getSession(sessionId);
if (!session.isPresent()) { // Java 9 has #ifPresentOrElse(...)
this.log.error("Unable to find session! [{}]", sessionId);
}
getSession(sessionId).ifPresent(s -> {
try {
s.getBasicRemote().sendObject(frame);
} catch (IOException | EncodeException e) {
this.log.error("Unable to send message! [sessionid={},command={}]", sessionId, frame.command(), e);
}
});
}
示例8: onError
import javax.websocket.EncodeException; //導入依賴的package包/類
@Test
public void onError() throws IOException, EncodeException {
final Session session = mock(Session.class);
when(session.getId()).thenReturn("sessionId");
final Throwable cause = new Throwable();
when(this.errorEvent.select(OnError.Literal.onError())).thenReturn(this.errorEvent);
final Basic basic = mock(Basic.class);
when(session.getBasicRemote()).thenReturn(basic);
this.endpoint.onError(session, cause);
verify(session).getId();
verify(session).getUserPrincipal();
verify(this.log).warn(eq("WebSocket error. [id={},principle={},errorId={}]"), eq("sessionId"), isNull(), anyString(), eq(cause));
verify(this.errorEvent).select(OnError.Literal.onError());
verify(this.errorEvent).fire(cause);
verify(session).getBasicRemote();
verify(basic).sendObject(any(Frame.class));
verify(session).close(any(CloseReason.class));
verifyNoMoreInteractions(session, basic);
}
示例9: fromBroker
import javax.websocket.EncodeException; //導入依賴的package包/類
@Test
public void fromBroker() throws IOException, EncodeException {
final Message msg = mock(Message.class);
when(msg.sessionId()).thenReturn("sessionId");
final Frame frame = mock(Frame.class);
when(msg.frame()).thenReturn(frame);
when(frame.command()).thenReturn(Command.MESSAGE);
final Session session = Mockito.mock(Session.class);
getSessionMap().put("sessionId", session);
getPrincipalSessionMap().put(NULL_PRINCIPLE, new HashSet<>(singleton(session)));
final Basic basic = mock(Basic.class);
when(session.getBasicRemote()).thenReturn(basic);
this.registry.fromBroker(msg);
verify(msg).sessionId();
verify(msg).frame();
verify(frame, times(2)).command();
verify(this.log).debug("Sending message to client. [sessionId={},command={}]", "sessionId", Command.MESSAGE);
verify(session).getBasicRemote();
verify(basic).sendObject(frame);
verifyNoMoreInteractions(msg, frame, session, basic);
}
示例10: fromBroker_ioe
import javax.websocket.EncodeException; //導入依賴的package包/類
@Test
public void fromBroker_ioe() throws IOException, EncodeException {
final Message msg = mock(Message.class);
when(msg.sessionId()).thenReturn("sessionId");
final Frame frame = mock(Frame.class);
when(msg.frame()).thenReturn(frame);
when(frame.command()).thenReturn(Command.MESSAGE);
final Session session = Mockito.mock(Session.class);
getSessionMap().put("sessionId", session);
getPrincipalSessionMap().put(NULL_PRINCIPLE, new HashSet<>(singleton(session)));
final Basic basic = mock(Basic.class);
when(session.getBasicRemote()).thenReturn(basic);
final IOException ioe = new IOException();
doThrow(ioe).when(basic).sendObject(frame);
this.registry.fromBroker(msg);
verify(msg).sessionId();
verify(msg).frame();
verify(frame, times(3)).command();
verify(this.log).debug("Sending message to client. [sessionId={},command={}]", "sessionId", Command.MESSAGE);
verify(session).getBasicRemote();
verify(basic).sendObject(frame);
verify(this.log).error("Unable to send message! [sessionid={},command={}]", "sessionId", Command.MESSAGE, ioe);
verifyNoMoreInteractions(msg, frame, session, basic);
}
示例11: onWebSocketText
import javax.websocket.EncodeException; //導入依賴的package包/類
@OnMessage
public void onWebSocketText(final Session sess, final JSONRPC2Message msg) throws IOException, EncodeException {
this.latestMessage = msg;
if (msg instanceof JSONRPC2Request) {
//All operations that are invokable on ContextManager that does not return void
System.out.println("The message is a Request " + msg.toJSONString());
final JSONRPC2Response data = new JSONRPC2Response(((JSONRPC2Request) msg).getID());
final Map<String,String> result = Maps.newHashMap();
result.put("decision", "valid");
result.put("reason", "");
data.setResult(result);
sess.getBasicRemote().sendObject(data);
}
else if (msg instanceof JSONRPC2Notification) {
//All operations that are invokable on ContextManager that does return void
System.out.println("The message is a Notification " + msg.toJSONString());
}
else if (msg instanceof JSONRPC2Response) {
//Can only be ContextChangesPending
System.out.println("The message is a Response " + msg.toJSONString());
}
}
示例12: sendMessageToSession
import javax.websocket.EncodeException; //導入依賴的package包/類
/**
* Try sending the {@link Message} using
* {@link Session#getBasicRemote()}, {@link Basic#sendObject(Object)}.
*
* @param session Session to send the message on
* @param message Message to send
* @return true if send was successful, or false if it failed
*/
private boolean sendMessageToSession(Session session, Message message) {
if (session.isOpen()) {
try {
session.getBasicRemote().sendObject(message);
return true;
} catch (EncodeException e) {
// Something was wrong encoding this message, but the connection
// is likely just fine.
Log.log(Level.FINE, this, "Unexpected condition writing message", e);
} catch (IOException ioe) {
// An IOException, on the other hand, suggests the connection is
// in a bad state.
Log.log(Level.FINE, this, "Unexpected condition writing message", ioe);
tryToClose(session, new CloseReason(CloseCodes.UNEXPECTED_CONDITION, trimReason(ioe.toString())));
}
}
return false;
}
示例13: encode
import javax.websocket.EncodeException; //導入依賴的package包/類
@Override
public String encode(AsciidocMessage m) throws EncodeException {
StringWriter swriter = new StringWriter();
try (JsonWriter jsonWrite = Json.createWriter(swriter)) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("type", m.getType().toString())
.add("adocId", m.getAdocId())
.add("data",
Json.createObjectBuilder()
.add("format", m.getFormat().toString())
.add("source", m.getAdocSource())
.add("sourceToMerge", m.getAdocSourceToMerge()!=null?m.getAdocSourceToMerge():""));
jsonWrite.writeObject(builder.build());
}
return swriter.toString();
}
示例14: encode
import javax.websocket.EncodeException; //導入依賴的package包/類
@Override
public String encode(NotificationMessage m) throws EncodeException {
StringWriter swriter = new StringWriter();
try (JsonWriter jsonWrite = Json.createWriter(swriter)) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("type", m.getType().toString()).
add("adocId", m.getAdocId()).
add(
"data",
Json.createObjectBuilder()
.add("nbConnected", m.getNbConnected())
.add("nbWriters", m.getWriters().size())
.add("writers", toJSON(m.getWriters())));
jsonWrite.writeObject(builder.build());
}
return swriter.toString();
}
示例15: receiveMessage
import javax.websocket.EncodeException; //導入依賴的package包/類
/**
* Called when a message is received. The WebSocket container will take
* data from the socket, and will transform it into the parameter EchoObject
* using the {@link EchoDecoder}.
* @param o Parameters converted into an EchoObject via the <code>EchoDecoder</code>
* @param session The session associated with this message
* @throws IOException
* @throws EncodeException
*/
@OnMessage
public void receiveMessage(EchoObject o, Session session)
throws IOException, EncodeException {
// Called when a message is received.
// Single endpoint per connection by default --> @OnMessage methods are single threaded!
// Endpoint/per-connection instances can see each other through sessions.
if (o.stopRequest()) {
session.close();
} else {
// Simple broadcast
for (Session s : session.getOpenSessions()) {
s.getBasicRemote().sendObject(o);
}
}
}