本文整理汇总了Java中javax.websocket.Session.isOpen方法的典型用法代码示例。如果您正苦于以下问题:Java Session.isOpen方法的具体用法?Java Session.isOpen怎么用?Java Session.isOpen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.websocket.Session
的用法示例。
在下文中一共展示了Session.isOpen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: broadcast
import javax.websocket.Session; //导入方法依赖的package包/类
public void broadcast(@Observes @LeaderDataQualifier String leaderboard) {
for (final Session s : CLIENTS) {
if (s != null && s.isOpen()) {
/**
* Asynchronous push
*/
s.getAsyncRemote().sendText(leaderboard, new SendHandler() {
@Override
public void onResult(SendResult result) {
if (result.isOK()) {
//Logger.getLogger(MeetupGroupsLiveLeaderboardEndpoint.class.getName()).log(Level.INFO, " sent to client {0}", s.getId());
} else {
Logger.getLogger(MeetupGroupsLiveLeaderboardEndpoint.class.getName()).log(Level.SEVERE, "Could not send to client " + s.getId(),
result.getException());
}
}
});
}
}
}
示例2: sendAddTimeRequest
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* Monitor has added one more minute to the time limit. All learners will need
* to add +1 minute to their countdown counters.
*/
private static void sendAddTimeRequest(Long toolContentId, int timeLimit) throws JSONException, IOException {
Set<Session> toolContentWebsockets = websockets.get(toolContentId);
if (toolContentWebsockets == null) {
return;
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("addTime", timeLimit);
String response = responseJSON.toString();
for (Session websocket : toolContentWebsockets) {
if (websocket.isOpen()) {
websocket.getBasicRemote().sendText(response);
}
}
}
示例3: sendPageRefreshRequest
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* Monitor has launched time limit. All learners will need to refresh the page in order to stop showing them
* waitForTimeLimitLaunch page.
*/
private static void sendPageRefreshRequest(Long toolContentId) throws JSONException, IOException {
Set<Session> toolContentWebsockets = websockets.get(toolContentId);
if (toolContentWebsockets == null) {
return;
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("pageRefresh", true);
String response = responseJSON.toString();
for (Session websocket : toolContentWebsockets) {
if (websocket.isOpen()) {
websocket.getBasicRemote().sendText(response);
}
}
}
示例4: send
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* Feeds opened websockets with commands.
*/
private void send(Long lessonId) throws IOException {
Long lastSendTime = lastSendTimes.get(lessonId);
if (lastSendTime == null) {
lastSendTime = System.currentTimeMillis() - CHECK_INTERVAL;
}
lastSendTimes.put(lessonId, System.currentTimeMillis());
List<Command> commands = CommandWebsocketServer.getLearnerService().getCommandsForLesson(lessonId,
new Date(lastSendTime));
Map<String, Session> lessonWebsockets = CommandWebsocketServer.websockets.get(lessonId);
for (Command command : commands) {
Session websocket = lessonWebsockets.get(command.getUserName());
if (websocket != null && websocket.isOpen()) {
websocket.getBasicRemote().sendText(command.getCommandText());
}
}
}
示例5: sendCloseRequest
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* The leader finished scratching and also . Non-leaders will have
* Finish button displayed.
*/
public static void sendCloseRequest(Long toolSessionId) throws JSONException, IOException {
Set<Session> sessionWebsockets = LearningWebsocketServer.websockets.get(toolSessionId);
if (sessionWebsockets == null) {
return;
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("close", true);
String response = responseJSON.toString();
for (Session websocket : sessionWebsockets) {
if (websocket.isOpen()) {
websocket.getBasicRemote().sendText(response);
}
}
}
示例6: sendCloseRequest
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* The scribe or a Monitor has force completed the activity. Browsers will refresh and display report summary.
*/
static void sendCloseRequest(Long toolSessionId) throws JSONException, IOException {
Set<Session> sessionWebsockets = LearningWebsocketServer.websockets.get(toolSessionId);
if (sessionWebsockets == null) {
return;
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("close", true);
String response = responseJSON.toString();
for (Session websocket : sessionWebsockets) {
if (websocket.isOpen()) {
websocket.getBasicRemote().sendText(response);
}
}
}
示例7: updateStatus
import javax.websocket.Session; //导入方法依赖的package包/类
public static synchronized void updateStatus(@Observes StatusEvent event) {
log.info("updateStatus");
for (Session s : sessions) {
if (s.isOpen()) {
try {
String studentUpdate = jsonStudentUpdate(event.getStudent());
s.getBasicRemote().sendText(studentUpdate);
log.log(Level.INFO, "[StatusEndpoint] {0} is now {1}",
new Object[]{event.getStudent().getName(),
event.getStudent().getStatus()});
/* Send update */
} catch (IOException e) {
log.log(Level.INFO, "[StatusEndpoint] {0}", e.getMessage());
}
}
}
}
示例8: updateStatus2
import javax.websocket.Session; //导入方法依赖的package包/类
public static synchronized void updateStatus2(Student student) {
log.info("updateStatus2");
for (Session s : sessions) {
if (s.isOpen()) {
try {
String studentUpdate = jsonStudentUpdate(student);
s.getBasicRemote().sendText(studentUpdate);
log.log(Level.INFO, "[StatusEndpoint] {0} is now {1}",
new Object[]{student.getName(),
student.getStatus()});
/* Send update */
} catch (IOException e) {
log.log(Level.INFO, "[StatusEndpoint] {0}", e.getMessage());
}
}
}
}
示例9: closeWebSocketConnection
import javax.websocket.Session; //导入方法依赖的package包/类
/**
* Close Websocket connection Connection
* @param session
*/
private void closeWebSocketConnection(final Session session ){
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
}
if (session != null && session.isOpen()) {
try {
CloseReason closeReason = new CloseReason(
CloseCodes.NORMAL_CLOSURE, "Session Closed");
session.close(closeReason);
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例10: closeWebSocketConnection
import javax.websocket.Session; //导入方法依赖的package包/类
/**
*
* Close websocket client connection.
* @param session
*/
public void closeWebSocketConnection(Session session){
try {
Thread.sleep(DELAY);
} catch (InterruptedException e1) {
}
if (session != null && session.isOpen()) {
try {
CloseReason closeReason = new CloseReason(CloseCodes.NORMAL_CLOSURE,"Closed");
session.close(closeReason);
logger.info("Session closed");
} catch (IOException e) {
logger.error("Fail to close connection ",e);
}
}
}
示例11: onMessage
import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void onMessage(Session session, String message) {
String messageToSend = message.toUpperCase();
try {
if (session.isOpen()) session.getBasicRemote().sendText(messageToSend);
} catch (Exception e) {
LOGGER.error("Failed to send message to client, message: {}, session: {}", messageToSend, session);
}
}
示例12: getOpenCount
import javax.websocket.Session; //导入方法依赖的package包/类
private int getOpenCount(Set<Session> sessions) {
int result = 0;
for (Session session : sessions) {
if (session.isOpen()) {
result++;
}
}
return result;
}
示例13: echoBinaryMessage
import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer bb,
boolean last) {
try {
if (session.isOpen()) {
session.getBasicRemote().sendBinary(bb, last);
}
} catch (IOException e) {
try {
session.close();
} catch (IOException e1) {
// Ignore
}
}
}
示例14: broadcastMessage
import javax.websocket.Session; //导入方法依赖的package包/类
public static void broadcastMessage(String message) {
for (Session s : sessions) {
if (s.isOpen()) {
try {
s.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例15: doMaxMessageSize
import javax.websocket.Session; //导入方法依赖的package包/类
private void doMaxMessageSize(String path, long size, boolean expectOpen)
throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMapping("/", "default");
tomcat.start();
WebSocketContainer wsContainer =
ContainerProvider.getWebSocketContainer();
Session s = connectToEchoServer(wsContainer, new EndpointA(), path);
// One for the client, one for the server
validateBackgroundProcessCount(2);
StringBuilder msg = new StringBuilder();
for (long i = 0; i < size; i++) {
msg.append('x');
}
s.getBasicRemote().sendText(msg.toString());
// Wait for up to 5 seconds for the client session to open
boolean open = s.isOpen();
int count = 0;
while (open != expectOpen && count < 50) {
Thread.sleep(100);
count++;
open = s.isOpen();
}
Assert.assertEquals(Boolean.valueOf(expectOpen),
Boolean.valueOf(s.isOpen()));
// Close the session if it is expected to be open
if (expectOpen) {
s.close();
}
// Ensure both server and client have shutdown
validateBackgroundProcessCount(0);
}