本文整理匯總了Java中org.eclipse.jetty.websocket.api.Session.isOpen方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.isOpen方法的具體用法?Java Session.isOpen怎麽用?Java Session.isOpen使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jetty.websocket.api.Session
的用法示例。
在下文中一共展示了Session.isOpen方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: broadcast
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
/** Broadcast text to all connected clients. */
void broadcast(String message, Session sourceSession) {
String sourceName = null;
synchronized(sessionToName) {
sourceName = sessionToName.get(sourceSession);
}
if (sourceName != null && !sourceName.isEmpty()) {
message = "<strong>" + sourceName + "</strong>: " + message;
}
synchronized(sessionToName) {
for (Session s : sessionToName.keySet()) {
try {
if (s.isOpen()) {
s.getRemote().sendStringByFuture(message);
}
} catch (WebSocketException e) {
System.out.println("The error " + e.getLocalizedMessage() + " occurred. This "
+ "can happen if the player dies before a broadcast arrives.");
}
}
}
}
示例2: send
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
public static boolean send(Session session, String msg) {
if (session == null)
return false;
try {
if (session.isOpen())
session.getRemote().sendString(msg);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
示例3: killRequestedJob
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
private void killRequestedJob(ExecutionStatus executionStatus, Session session) {
if (Constants.KILL.equalsIgnoreCase(executionStatus.getType())) {
logger.info("Kill request received for job - " + executionStatus.getJobId());
final String jobId = executionStatus.getJobId().trim();
for (Session openSession : allSessions.keySet()) {
if (openSession.isOpen()) {
try {
if (allSessions.get(openSession).get(Constants.CLIENTID) != null) {
if (((String) allSessions.get(openSession).get(Constants.CLIENTID))
.equalsIgnoreCase(Constants.ENGINE_CLIENT + executionStatus.getJobId())) {
logger.debug("Before sending kill" + jobId);
openSession.getRemote().sendStringByFuture("");
logger.debug("After sending kill" + jobId);
}
}
} catch (Exception e) {
logger.error("Failed to send kill request for - " + jobId, e);
}
}
}
}
}
示例4: removeClosedSessions
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
/**
* Remove closed sessions from listed sessions. If any client disconnects
* from the websocket he will be removed from the list of connected clients.
*/
@Override
public void removeClosedSessions() {
// New set because otherwise data wouldn't be copied
Set<Integer> clientIdsBefore = new HashSet<>();
clientIdsBefore.addAll(sessions.keySet());
boolean removedSomething = false;
Iterator<Session> iter = sessions.values().iterator();
while (iter.hasNext()) {
Session session = iter.next();
if (!session.isOpen()) {
LOGGER.info("Remove session: "
+ session.getRemoteAddress().toString());
iter.remove();
removedSomething = true;
}
}
if(removedSomething){
// Remove unused clientIds from ClientInformation
Set<Integer> clientIdsAfterwards = sessions.keySet();
for(int clientId : clientIdsBefore){
if(! clientIdsAfterwards.contains(clientId)){
clientInformationHandler.removeConnection(clientId);
}
}
// Notify Observers, e.g. Developer Settings Handler so that the connected users list can be updated
setChanged();
notifyObservers();
}
}
示例5: onWebSocketText
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
/**
* Called by Jetty when a text frame is received from the WebSocket client.
*/
@Override
public void onWebSocketText(String message) {
log.info("Received unsupported text message from peer {}", peerID);
Session session = getSession();
if (session != null && session.isOpen()) {
session.close(StatusCode.BAD_DATA, "This endpoint only accepts binary messages");
}
}
示例6: offerMessage
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
/**
* @see ClientConnection#offerMessage(ServerToClient)
*/
public boolean offerMessage(ServerToClient message) {
Session session = getSession();
if (session == null || !session.isOpen()) return false;
if (inFlightMessages.incrementAndGet() > MAX_IN_FLIGHT_MESSAGES) {
inFlightMessages.decrementAndGet();
return false;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(stream, null);
writeToClient.write(message, encoder);
encoder.flush();
} catch (Exception e) {
log.warn("Failed to encode message to client", e);
session.close(StatusCode.SERVER_ERROR, "Internal server error");
return true;
}
session.getRemote().sendBytes(ByteBuffer.wrap(stream.toByteArray()), new WriteCallback() {
@Override
public void writeSuccess() {
inFlightMessages.decrementAndGet();
}
@Override
public void writeFailed(Throwable error) {
inFlightMessages.decrementAndGet();
log.info("Sending message to WebSocket client failed: ", error);
}
});
return true;
}
示例7: unicast
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
/** Send text to a single client */
void unicast(String message, Session session) {
try {
if (session.isOpen()) {
session.getRemote().sendStringByFuture(message);
}
} catch (WebSocketException e) {
System.out.println("The error " + e.getLocalizedMessage() + " occurred. This "
+ "can happen if the player dies before a unicast arrives.");
}
}
示例8: onText
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
@OnWebSocketMessage
public void onText(Session session, String message) throws IOException {
if (session.isOpen()) {
String response = message.toUpperCase();
session.getRemote().sendString(response);
}
}
示例9: onConnect
import org.eclipse.jetty.websocket.api.Session; //導入方法依賴的package包/類
@OnWebSocketConnect
public void onConnect(Session session) throws IOException {
User user = null;
String authorizationHeader = session.getUpgradeRequest().getHeader("Authorization");
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
if (!session.getUpgradeRequest().getQueryString().isEmpty()) {
Map<String, String> queryMap = splitQuery(session.getUpgradeRequest().getQueryString());
if (queryMap.containsKey("key")) {
user = userManager.getApiKeyManager().getByAPIKey(queryMap.get("key"));
}
}
if (user == null && userManager.getUser("default").isPresent()) {
user = userManager.getUser("default").get();
} else if (user == null) {
logger.debug("No authentication found and no default user found for session {}",session.toString());
if (session.isOpen()) {
session.getRemote().sendString(
getErrorResponse(ErrorCodes.INVALID_CREDENTIALS, "No authentication found and no default user found"),
new CloseCallback(session, 401, "No authentication found and no default user found")
);
}
return;
}
} else {
if (!authorizationHeader.startsWith("Basic")) {
logger.debug("Invalid authorization header for session {}", session);
if (session.isOpen()) {
session.getRemote().sendString(
getErrorResponse(ErrorCodes.INVALID_AUTHORIZATION_HEADER, "Invalid Authorization header"),
new CloseCallback(session, 401, "Invalid Authorization header")
);
}
return;
}
String base64Credentials = authorizationHeader.substring("Basic".length()).trim();
String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8"));
final String[] values = credentials.split(":", 2);
if (values.length != 2) {
logger.debug("Invalid authorization header for session {}", session);
if (session.isOpen()) {
session.getRemote().sendString(
getErrorResponse(ErrorCodes.INVALID_AUTHORIZATION_HEADER, "Invalid Authorization header"),
new CloseCallback(session, 401, "Invalid Authorization header")
);
}
return;
}
String username = values[0];
String password = values[1];
if (!userManager.checkCredentials(username, password)) {
logger.debug("Invalid credentials for session {}", session);
if (session.isOpen()) {
session.getRemote().sendString(
getErrorResponse(ErrorCodes.INVALID_CREDENTIALS, "Invalid credentials"),
new CloseCallback(session, 401, "Invalid credentials")
);
}
return;
}
user = userManager.getUser(username).get();
}
invokers.put(session, new WebSocketInvoker(user, session));
}
開發者ID:UniversalMinecraftAPI,項目名稱:UniversalMinecraftAPI,代碼行數:70,代碼來源:UniversalMinecraftAPIWebSocket.java