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


Java Session.getNegotiatedSubprotocol方法代码示例

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


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

示例1: handleMessage

import javax.websocket.Session; //导入方法依赖的package包/类
@OnMessage
public String handleMessage(Session session, final String message, @PathParam("user") String user) {
	String proto = session.getNegotiatedSubprotocol();
	return message + " " + user + (proto.isEmpty() ? "" : " (protocol=" + proto + ")");
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:6,代码来源:MessageEndpoint.java

示例2: testWsSubprotocols

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

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketContainer wsContainer = ContainerProvider
            .getWebSocketContainer();

    tomcat.start();

    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
                    .create().preferredSubprotocols(Arrays.asList("sp3"))
                    .build(), new URI("ws://localhost:" + getPort()
                    + SubProtocolsEndpoint.PATH_BASIC));

    Assert.assertTrue(wsSession.isOpen());
    if (wsSession.getNegotiatedSubprotocol() != null) {
        Assert.assertTrue(wsSession.getNegotiatedSubprotocol().isEmpty());
    }
    wsSession.close();
    SubProtocolsEndpoint.recycle();

    wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
                    .create().preferredSubprotocols(Arrays.asList("sp2"))
                    .build(), new URI("ws://localhost:" + getPort()
                    + SubProtocolsEndpoint.PATH_BASIC));

    Assert.assertTrue(wsSession.isOpen());
    Assert.assertEquals("sp2", wsSession.getNegotiatedSubprotocol());
    // Client thread might move faster than server. Wait for upto 5s for the
    // subProtocols to be set
    int count = 0;
    while (count < 50 && SubProtocolsEndpoint.subprotocols == null) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertNotNull(SubProtocolsEndpoint.subprotocols);
    Assert.assertArrayEquals(new String[]{"sp1","sp2"},
            SubProtocolsEndpoint.subprotocols.toArray(new String[2]));
    wsSession.close();
    SubProtocolsEndpoint.recycle();
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:52,代码来源:TestWsSubprotocols.java


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