2017-01-16 16 views
13

Dựa trên this question Tôi muốn tạo một cá thể điểm cuối máy chủ dựa trên subprotocol được thương lượng để xử lý các thông báo giao thức khác nhau. Thật không may ServerEndpointConfig.Configurator.getEndpointInstance [docs] sẽ không cho phép tôi truy cập bất kỳ dữ liệu phiên có liên quan nào để nhận được subprotol được thương lượng để tôi có thể khởi tạo các lớp khác nhau.Websocket ServerĐối tượng địa lý của subprotocol

public static class ServerEndpointConfigurator extends 
     ServerEndpointConfig.Configurator { 

    public ServerEndpointConfigurator() 
    { 
    } 

    @Override 
    public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { 
     // useful to work with session data in endpoint instance but not at getEndpointInstance 
     HttpSession httpSession = (HttpSession) request.getHttpSession(); 
     config.getUserProperties().put(HttpSession.class.getName(), httpSession); 
    } 

    @Override 
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { 

     // TODO get negotiated subprotocol and instantiate endpoint using switch case or factory 

     return (T) new WebSocketControllerA(); 

     // or return (T) new WebSocketControllerB(); 
     // or return (T) new WebSocketControllerC(); 
     // ... 
    } 
} 

Bất kỳ ý tưởng nào về cách giải quyết vấn đề này hoặc có bất kỳ thực tiễn được chấp nhận rộng rãi nào về cách xử lý các giao thức con khác nhau? Tôi đang gặp khó khăn trong việc tìm kiếm các triển khai ví dụ hoặc tài liệu nâng cao về xử lý subprotocol trên web.

Trả lời

0

Đây có phải là những gì bạn đang tìm kiếm không?

@ServerEndpoint("/ws") 
public class MyWebSocket { 

    @OnOpen 
    public void onOpen(Session session) { 
     session.getNegotiatedSubprotocol(); 
    } 
Các vấn đề liên quan