Skip to content

Commit

Permalink
add: websocket把HttpSession也赋予给WsHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed May 8, 2017
1 parent 792143e commit 1189ebc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nutz-plugins-websocket/pom.xml
Expand Up @@ -122,5 +122,11 @@
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
Expand Down Expand Up @@ -70,6 +71,7 @@ public void onOpen(Session session, EndpointConfig config) {
WsHandler handler = createHandler(session, config);
handler.setRoomProvider(roomProvider);
handler.setSession(session);
handler.setHttpSession((HttpSession)config.getUserProperties().get("HttpSession"));
session.addMessageHandler(handler);
sessions.put(wsid, session);
handlers.put(wsid, handler);
Expand Down
@@ -1,5 +1,7 @@
package org.nutz.plugins.mvc.websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import org.nutz.ioc.Ioc;
Expand All @@ -18,5 +20,12 @@ public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationExc
ioc = Mvcs.ctx().getDefaultIoc();
return ioc.get(endpointClass);
}

public void modifyHandshake(ServerEndpointConfig sec,
HandshakeRequest request,
HandshakeResponse response) {
super.modifyHandshake(sec, request, response);
sec.getUserProperties().put("HttpSession", request.getHttpSession());
}
}

@@ -1,12 +1,15 @@
package org.nutz.plugins.mvc.websocket;

import javax.servlet.http.HttpSession;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

public interface WsHandler extends MessageHandler {

void setSession(Session session);

void setHttpSession(HttpSession httpSession);

void setRoomProvider(WsRoomProvider roomProvider);

void onMessage(String msg);
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.nutz.lang.Strings;
Expand All @@ -21,6 +22,7 @@ public abstract class AbstractWsHandler implements WsHandler {

protected Session session;
protected String prefix;
protected HttpSession httpSession;

public AbstractWsHandler(String prefix) {
rooms = new HashSet<>();
Expand Down Expand Up @@ -58,4 +60,8 @@ public void setRoomProvider(WsRoomProvider roomProvider) {
public void setSession(Session session) {
this.session = session;
}

public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
}

0 comments on commit 1189ebc

Please sign in to comment.