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


Java Session.close方法代码示例

本文整理汇总了Java中javax.websocket.Session.close方法的典型用法代码示例。如果您正苦于以下问题:Java Session.close方法的具体用法?Java Session.close怎么用?Java Session.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.websocket.Session的用法示例。


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

示例1: echoTester

import javax.websocket.Session; //导入方法依赖的package包/类
public void echoTester(String path) throws Exception {
    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig =
            ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class,
            clientEndpointConfig,
            new URI("ws://localhost:" + getPort() + path));
    CountDownLatch latch =
            new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");

    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);

    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    for (String message : messages) {
        Assert.assertEquals("Hello", message);
    }
    wsSession.close();
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:26,代码来源:TestWebSocketFrameClient.java

示例2: echoBinaryMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer msg,
        boolean last) {
    try {
        session.getBasicRemote().sendBinary(msg, last);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:14,代码来源:TesterEchoServer.java

示例3: userConnectedCallback

import javax.websocket.Session; //导入方法依赖的package包/类
@OnOpen
public void userConnectedCallback(@PathParam("user") String user, Session s) {

    if (USERS.contains(user)) {
        try {
            dupUserDetected = true;
            s.getBasicRemote().sendObject(new DuplicateUserNotification(user));
            s.close();
            return;
        } catch (Exception ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    this.s = s;
    SESSIONS.add(s);
    s.getUserProperties().put("user", user);
    this.user = user;
    USERS.add(user);

    welcomeNewJoinee();
    announceNewJoinee();
}
 
开发者ID:abhirockzz,项目名称:scalable-websocket-chat-with-hazelcast,代码行数:25,代码来源:ChatServer.java

示例4: testClientDropsConnection

import javax.websocket.Session; //导入方法依赖的package包/类
@Test
public void testClientDropsConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();

    tomcat.start();

    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);

    Session session = wsContainer.connectToServer(client, uri);
    // Break point A required on following line
    session.close();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:22,代码来源:TestWsRemoteEndpointImplServer.java

示例5: onMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void onMessage(Session session, String msg) throws IOException {

    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        } else {
            started = true;
        }
    }

    System.out.println("Received " + msg + ", now sending data");

    session.getUserProperties().put(
            "org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT",
            Long.valueOf(SEND_TIME_OUT_MILLIS));

    Basic remote = session.getBasicRemote();
    remote.setBatchingAllowed(true);

    for (int i = 0; i < MESSAGE_COUNT; i++) {
        remote.sendText(MESSAGE);
        if (i % (MESSAGE_COUNT * 0.4) == 0) {
            remote.setBatchingAllowed(false);
            remote.setBatchingAllowed(true);
        }
    }

    // Flushing should happen automatically on session close
    session.close();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:35,代码来源:TesterFirehoseServer.java

示例6: echoTextMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    try {
        if (session.isOpen()) {
            session.getBasicRemote().sendText(msg, last);
        }
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:15,代码来源:TesterWsClientAutobahn.java

示例7: echoTextMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public void echoTextMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:13,代码来源:TesterEchoServer.java

示例8: echoTextMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage(maxMessageSize = MAX_SIZE)
public void echoTextMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:13,代码来源:TesterEchoServer.java

示例9: handleOnOpenError

import javax.websocket.Session; //导入方法依赖的package包/类
private void handleOnOpenError(Session session, Throwable t) {
	// If really fatal - re-throw
	ExceptionUtils.handleThrowable(t);

	// Trigger the error handler and close the session
	onError(session, t);
	try {
		session.close();
	} catch (IOException ioe) {
		log.warn(sm.getString("pojoEndpointBase.closeSessionFail"), ioe);
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:13,代码来源:PojoEndpointBase.java

示例10: handleOnOpenError

import javax.websocket.Session; //导入方法依赖的package包/类
private void handleOnOpenError(Session session, Throwable t) {
    // If really fatal - re-throw
    ExceptionUtils.handleThrowable(t);

    // Trigger the error handler and close the session
    onError(session, t);
    try {
        session.close();
    } catch (IOException ioe) {
        log.warn(sm.getString("pojoEndpointBase.closeSessionFail"), ioe);
    }
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:13,代码来源:PojoEndpointBase.java

示例11: testUnsupportedObject

import javax.websocket.Session; //导入方法依赖的package包/类
@Test
public void testUnsupportedObject() throws Exception{
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgramaticServerEndpointConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);

    // This should fail
    Object msg1 = new Object();
    try {
        session.getBasicRemote().sendObject(msg1);
        Assert.fail("No exception thrown ");
    } catch (EncodeException e) {
        // Expected
    } catch (Throwable t) {
        Assert.fail("Wrong exception type");
    } finally {
        session.close();
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:31,代码来源:TestEncodingDecoding.java

示例12: open

import javax.websocket.Session; //导入方法依赖的package包/类
@OnOpen
public void open(Session session, @PathParam(value = "user")String user) {
    Session session1 = sessionMap.get(user);
    if (null != session1) {
        try {
            session1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    sessionMap.put(user, session);
    log.info("*** WebSocket opened from sessionId " + session.getId());
}
 
开发者ID:yangjinhe,项目名称:maintain-robot,代码行数:14,代码来源:WebSocketModule.java

示例13: testBug56032

import javax.websocket.Session; //导入方法依赖的package包/类
@Test
public void testBug56032() throws Exception {
    // TODO Investigate options to get this test to pass with the HTTP BIO
    //      connector.
    Assume.assumeFalse(
            "Skip this test on BIO. TODO: investigate options to make it pass with HTTP BIO connector",
            getTomcatInstance().getConnector().getProtocolHandlerClassName().equals(
                    "org.apache.coyote.http11.Http11Protocol"));

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    TesterSupport.initSsl(tomcat);

    tomcat.start();

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig =
            ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(
            WsWebSocketContainer.SSL_TRUSTSTORE_PROPERTY,
            "test/org/apache/tomcat/util/net/ca.jks");
    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class,
            clientEndpointConfig,
            new URI("wss://localhost:" + getPort() +
                    TesterFirehoseServer.Config.PATH));

    // Process incoming messages very slowly
    MessageHandler handler = new SleepingText(5000);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");

    // Wait long enough for the buffers to fill and the send to timeout
    int count = 0;
    int limit = TesterFirehoseServer.WAIT_TIME_MILLIS / 100;

    System.err.println("Waiting for server to report an error");
    while (TesterFirehoseServer.Endpoint.getErrorCount() == 0 && count < limit) {
        Thread.sleep(100);
        count ++;
    }

    if (TesterFirehoseServer.Endpoint.getErrorCount() == 0) {
        Assert.fail("No error reported by Endpoint when timeout was expected");
    }

    // Wait up to another 20 seconds for the connection to be closed
    System.err.println("Waiting for connection to be closed");
    count = 0;
    limit = (TesterFirehoseServer.SEND_TIME_OUT_MILLIS * 4) / 100;
    while (TesterFirehoseServer.Endpoint.getOpenConnectionCount() != 0 && count < limit) {
        Thread.sleep(100);
        count ++;
    }

    int openConnectionCount = TesterFirehoseServer.Endpoint.getOpenConnectionCount();
    if (openConnectionCount != 0) {
        Assert.fail("There are [" + openConnectionCount + "] connections still open");
    }

    // Close the client session.
    wsSession.close();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:70,代码来源:TestWebSocketFrameClientSSL.java

示例14: testProgrammaticEndPoints

import javax.websocket.Session; //导入方法依赖的package包/类
@Test
public void testProgrammaticEndPoints() throws Exception{
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(
            ProgramaticServerEndpointConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);

    MsgString msg1 = new MsgString();
    msg1.setData(MESSAGE_ONE);
    session.getBasicRemote().sendObject(msg1);
    // Should not take very long
    int i = 0;
    while (i < 20) {
        if (MsgStringMessageHandler.received.size() > 0 &&
                client.received.size() > 0) {
            break;
        }
        Thread.sleep(100);
        i++;
    }

    // Check messages were received
    Assert.assertEquals(1, MsgStringMessageHandler.received.size());
    Assert.assertEquals(1, client.received.size());

    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE,
            ((MsgString) MsgStringMessageHandler.received.peek()).getData());
    Assert.assertEquals(MESSAGE_ONE,
            new String(((MsgByte) client.received.peek()).getData()));
    session.close();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:44,代码来源:TestEncodingDecoding.java

示例15: testOnErrorNotCalledWhenClosingConnection

import javax.websocket.Session; //导入方法依赖的package包/类
@Test
public void testOnErrorNotCalledWhenClosingConnection() throws Throwable {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624ServerConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Bug58624ClientEndpoint client = new Bug58624ClientEndpoint();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624ServerConfig.PATH);

    Session session = wsContainer.connectToServer(client, uri);

    // Wait for session to open on the server
    int count = 0;
    while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() == 0) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertNotEquals(0,  Bug58624ServerEndpoint.getOpenSessionCount());

    // Now close the session
    session.close();

    // Wait for session to close on the server
    count = 0;
    while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() > 0) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertEquals(0,  Bug58624ServerEndpoint.getOpenSessionCount());

    // Ensure no errors were reported on the server
    Assert.assertEquals(0,  Bug58624ServerEndpoint.getErrorCount());

    if (client.getError() != null) {
        throw client.getError();
    }
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:45,代码来源:TestCloseBug58624.java


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