@Override public InetSocketAddress call() throws Exception { return (InetSocketAddress) endpointConfig.getUserProperties().get(JAVAX_WEBSOCKET_ENDPOINT_LOCAL_ADDRESS); } })
@Override public InetSocketAddress call() throws Exception { return (InetSocketAddress) endpointConfig.getUserProperties().get(JAVAX_WEBSOCKET_ENDPOINT_REMOTE_ADDRESS); } })
@OnOpen public void onOpen(Session session, EndpointConfig config) { User user = (User) config.getUserProperties().get("user"); // ... }
@ServerEndpoint(value="/push", configurator=UserAwareConfigurator.class) public class PushEndpoint { @Inject private PushContext pushContext; @OnOpen public void onOpen(Session session, EndpointConfig config) { User user = (User) config.getUserProperties().get("user"); pushContext.add(session, user); } @OnClose public void onClose(Session session) { pushContext.remove(session); } }
@OnOpen public void onOpen(final Session session, EndpointConfig config) { Principal userPrincipal = (Principal) config.getUserProperties().get("UserPrincipal"); Boolean userInRole = (Boolean) config.getUserProperties().get("userInRole"); //do what ever you like with it }
import javax.websocket.EndpointConfig; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value = "/foo", configurator = MyServerEndpointConfigurator.class) public class MyEndpoint { @OnOpen public void onOpen(Session session, EndpointConfig endpointConfig) throws Exception { String ip = ((TyrusSession) session).getRemoteAddr(); String userAgent = (String) endpointConfig.getUserProperties().get("user-agent"); ... } }
@SuppressWarnings("unchecked") @Override public final void init(final EndpointConfig config) { this.unmarshaller = (Supplier<Unmarshaller>) config.getUserProperties().get(UserProperties.UNMARSHALLER); this.onRead = (BiConsumer<String, StreamElement>) config.getUserProperties().get(UserProperties.ON_READ); }
@ServerEndpoint (value = "/myWebSocketEndpoint", encoders={WSEncoder.class}, decoders={WSDecoder.class}, configurator=WebsocketEndpointConfigurator.class) public class WebsocketServer{ @OnOpen public void onOpen(Session session, EndpointConfig config) { this.wsSession=session; this.httpSession=(HttpSession) config.getUserProperties().get(HttpSession.class.getName()); String foo = (String) httpSession.getAttribute("foo"); } }
@ServerEndpoint(value = "/chatroomServerEndpoint/{chatroom}",configurator=ChatRoomEndpointConfigurator.class) public class WSServer { protected Session wsSession; protected HttpSession httpSession; //Now, you can access the http's session attributes: @OnOpen public void onOpen(Session session, EndpointConfig config) { this.wsSession=session; this.httpSession=(HttpSession) config.getUserProperties().get(HttpSession.class.getName()); String chatRoom = (String) httpSession.getAttribute("chatRoom"); } }
@SuppressWarnings("unchecked") @Override public final void init(final EndpointConfig config) { this.xmlOutputFactory = (XMLOutputFactory) config.getUserProperties().get(UserProperties.XML_OUTPUT_FACTORY); if (xmlOutputFactory == null) { xmlOutputFactory = XMLOutputFactory.newFactory(); } this.marshaller = (Supplier<Marshaller>) config.getUserProperties().get(UserProperties.MARSHALLER); this.interceptor = (BiConsumer<String, StreamElement>) config.getUserProperties().get(UserProperties.ON_WRITE); }
/** * * 连接建立成功调用的方法 * * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 * @param config 端点配置 */ @OnOpen public void onOpen(Session session, EndpointConfig config) { httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName()); this.session = session; items.put(httpSession.getId(), this); log.debug("有新连接加入!当前在线人数为 {}", items.size()); log.debug("sessionId: {}", httpSession.getId()); }
@ServerEndpoint(value = "/chat/{room}", configurator = GetHttpSessionConfigurator.class, encoders = ChatMessageEncoder.class, decoders = ChatMessageDecoder.class) public class ChatEndpoint { private final Logger log = Logger.getLogger(getClass().getName()); @OnOpen public void open(final Session session, @PathParam("room") final String room, EndpointConfig config) { log.info("session openend and bound to room: " + room); Principal userPrincipal = (Principal) config.getUserProperties().get( "UserPrincipal"); String user=null; try { user = (String) userPrincipal.getName(); } catch (Exception e) { e.printStackTrace(); } session.getUserProperties().put("user", user); System.out.println("!!!!!!!! "+user); }}
@Override public void onOpen(Session session, EndpointConfig config) { log.trace("Session {} opened", session.getId()); // Set maximum messages size to 10,000 bytes session.setMaxTextMessageBufferSize(10000); session.addMessageHandler(stringHandler); session.addMessageHandler(binaryHandler); // get the manager manager = (WebSocketScopeManager) config.getUserProperties().get(WSConstants.WS_MANAGER); // get ws scope from user props scope = (WebSocketScope) config.getUserProperties().get(WSConstants.WS_SCOPE); }
@OnOpen public void onOpen(Session session, EndpointConfig config) { final HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName()); final ErraiWebSocketChannel channel = ErraiChannelFactory.getInstance().buildWebsocketChannel(session, httpSession); CHANNELS.put(session.getId(), channel); }
@OnOpen public void onOpen(Session session, EndpointConfig config) { final HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName()); final ErraiWebSocketChannel channel = ErraiChannelFactory.getInstance().buildWebsocketChannel(session, httpSession); CHANNELS.put(session.getId(), channel); }
public JavaxUpgradeHttpRequest(final Session session, EndpointConfig endpointConfig) { Map<String, Object> userProperties = session.getUserProperties(); if (userProperties == null || userProperties.isEmpty()) { // workaround for JBoss Wildfly 8.0.Final userProperties = endpointConfig.getUserProperties(); } this.httpSession = (HttpSession) userProperties.get("session"); this.headers = (Map<String, List<String>>) userProperties.get("headers"); this.queryString = session.getQueryString(); this.userPrincipal = session.getUserPrincipal(); Object requestURI = session.getRequestURI(); this.requestUri = requestURI != null ? requestURI.toString() : ""; this.parametersMap = new HashMap<>(); Map<String, List<String>> parameters = session.getRequestParameterMap(); if (parameters != null) { for (Map.Entry<String, List<String>> entry : parameters.entrySet()) { String name = entry.getKey(); List<String> value = entry.getValue(); parametersMap.put(name, value.toArray(new String[0])); } } }
type = Type.class.cast(endpointConfig.getUserProperties().get("johnzon.websocket.message.type")); if (type == null) { throw new IllegalArgumentException("didn't find johnzon.websocket.message.type");
public JavaxUpgradeHttpRequest(final Session session, EndpointConfig endpointConfig) { Map<String, Object> userProperties = session.getUserProperties(); if (userProperties == null || userProperties.isEmpty()) { // workaround for JBoss Wildfly 8.0.Final userProperties = endpointConfig.getUserProperties(); } this.httpSession = (HttpSession) userProperties.get("session"); this.headers = (Map<String, List<String>>) userProperties.get("headers"); this.queryString = session.getQueryString(); this.userPrincipal = session.getUserPrincipal(); Object requestURI = session.getRequestURI(); this.requestUri = requestURI != null ? requestURI.toString() : ""; this.parametersMap = new HashMap<>(); Map<String, List<String>> parameters = session.getRequestParameterMap(); if (parameters != null) { for (Map.Entry<String, List<String>> entry : parameters.entrySet()) { String name = entry.getKey(); List<String> value = entry.getValue(); parametersMap.put(name, value.toArray(new String[0])); } } }
@Override public void onOpen(final Session session, final EndpointConfig config) { final RemoteEndpoint.Async remote = session.getAsyncRemote(); final URI address = (URI) config.getUserProperties().get("address"); transport = WebsocketTransportBuilder.get(address);
if (Boolean.TRUE.equals(config.getUserProperties().get(WebsocketConfigurator.WEBSOCKET_VALID)) && WebsocketApplicationSessionHolder.addOrUpdateSession(channelToken, session)) session.setMaxIdleTimeout((Long) config.getUserProperties().getOrDefault( WebsocketConfigurator.MAX_IDLE_TIMEOUT, 300000L));