Android Autobahn请求。getHttpSession()=null
客户端代码:Android Autobahn请求。getHttpSession()=null,android,websocket,request,autobahn,Android,Websocket,Request,Autobahn,客户端代码: private void startPolling() { final String wsuri = MainAsync.WEB_SOCKET_HOST_WS+"/game"; try { mConnection.connect(wsuri, new WebSocketHandler() { @Override public void onOpen() {
private void startPolling() {
final String wsuri = MainAsync.WEB_SOCKET_HOST_WS+"/game";
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri);
int role = mAccMeth.loadId(getApplicationContext());
mConnection.sendTextMessage(String.valueOf(role));
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
服务器端实现:
@ServerEndpoint(value="/game",configurator = ServletConfig.class)
public class WebSocketPollingCoordinates {
EndpointConfig endConfig;
@OnMessage
public void receiveMessage(String message,final Session session){
HttpSession httpSession = (HttpSession) endConfig.getUserProperties().get("httpSession");
//at this line i get NullpointerException
final ServletContext sercntxt = (ServletContext)httpSession.getServletContext();
}
@OnOpen
public void onOpen(Session sn, EndpointConfig ec) {
System.out.println("onOpen");
this.endConfig = ec;
}
@OnError
public void error(Session session, Throwable t) {
t.printStackTrace();
}
和WebSocket配置类:
公共类ServletConfig扩展配置程序{
@Override
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
HttpSession httpSession = (HttpSession) request.getHttpSession();
if(httpSession==null || config==null){
if(httpSession==null)
System.out.println("httpSession==null");
if(config==null)
System.out.println("config==null");
return;
}
config.getUserProperties().put("httpSession", httpSession);
}
}
我总是得到下一个结果:http会话==null。错误在哪里?我需要获取httpSession,以调用voidhttpSession.getServletContext();