基于这个问题,我想基于协商的子协议创建一个服务器端点实例,以不同的方式处理各种协议消息.不幸的是ServerEndpointConfig.Configurator.getEndpointInstance[ docs ]不允许我访问任何相关的会话数据来获得协商的子协议,所以我可以实例化不同的类.
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) …Run Code Online (Sandbox Code Playgroud) 我想在websocket handshake\session与HttpSession对象之间建立连接.
我使用了以下握手修改:
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response)
{
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
Run Code Online (Sandbox Code Playgroud)
正如本文所述: 在Web Socket @ServerEndpoint中从HttpServletRequest访问HttpSession
现在,由于某些原因在握手时,(HttpSession)request.getHttpSession()一直返回null.
这是我的客户端代码:
<!DOCTYPE html>
<html>
<head>
<title>Testing websockets</title>
</head>
<body>
<div>
<input type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket =
new WebSocket('ws://localhost:8080/com-byteslounge-websockets/websocket');
webSocket.onerror = function(event) {
onError(event)
};
webSocket.onopen = function(event) {
onOpen(event)
};
webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
document.getElementById('messages').innerHTML
+= '<br …Run Code Online (Sandbox Code Playgroud) 我已经遵循了很多教程和示例代码示例,但我还没有看到一种方法来访问客户端的HTTP头,主机名等,就像我们在Servlet的请求对象中一样.
我该怎么办呢?
假设我已经打开定义为 -
@OnOpen
public void onOpen(Session session) {
}
Run Code Online (Sandbox Code Playgroud)
在上面的方法中,有没有办法可以使用会话字段访问底层的HTTP连接细节?我很好,即使我可以获得底层Servlet(如果有的话)
我在我的应用程序中使用基于JavaEE 7的Websocket-API.
我需要访问我的websocket端点中的cookie中设置的值[Annotated one : @ServerEndpoint ("/websocket") ].我该怎么办?
@onOpen()方法就在那里,当建立与此websocket端点的连接时,将自动调用该方法.我想在这个方法中访问那里的cookie值.
我知道如何在servlet或JSP中做到这一点,但我是Websockets的新手.
请帮我这样做.提前致谢.
我制作了一个JSR-356 @ServerEndpoint,其中我想限制来自单个IP地址的活动连接,以防止简单的DDOS攻击.
请注意,我正在搜索Java解决方案(JSR-356,Tomcat或Servlet 3.0规范).
我尝试过自定义端点配置程序,但即使在HandshakeRequest对象中也无法访问IP地址.
如何在没有iptables等外部软件的情况下限制单个IP地址的JSR-356连接数?
我可以在类的@OnOpen方法中获取远程 IP 地址@ServletEndpoint吗?我尝试按照这个从 HttpServletRequest 在 Web 套接字 @SocketEndpoint 中访问 HttpSession但就像在Websocket 中一样 - httpSession 返回 null它不起作用。
无论如何,我只需要客户端 IP 地址。
如何访问 Jetty 9 中带注释的 @WebSocket 类中的 HttpSession 对象?
我找到了如何使用 @ServerEndpoint 注释来做到这一点,就像这里:HttpSession from @ServerEndpoint
使用@WebSocket 注释,就像在下面的类中一样,我该怎么做?
@WebSocket
public class AuctionWebSocket {
// NEED TO ACCESS HttpSession OBJECT INSIDE THESE METHODS:
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("onConnect...");
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("Message: " + message);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("onClose...");
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("onError...");
}
}
Run Code Online (Sandbox Code Playgroud)
在该方法中onConnect(Session session),我尝试调用session.getUpgradeRequest().getSession()which 始终返回null。
为了提供信息,以下是我如何启动嵌入式 Jetty 9: …
所以,我正在尝试解析一个使用websocket向客户端发送数据的网站.从我读过的内容:
我知道,为了创建连接,我需要向服务器发送一个特殊的http请求,然后我将检索一些用于访问该通道的数据.
我做了什么:
创建了http请求
`URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Host", "www.example.com");
con.setRequestProperty("Upgrade", "websocket");
con.setRequestProperty("Connection", "upgrade");
con.setRequestProperty("Origin", "https://example.com");
con.setRequestProperty("Sec-WebSocket-Extensions", "x-webkit-deflate-frame");
con.setRequestProperty("Sec-WebSocket-Key", "QkWkChtTGpaZL5PkOMaRtg==");
con.setRequestProperty("Sec-WebSocket-Version", "13");
con.setRequestProperty("User-Agent", "Mozilla/..;
Run Code Online (Sandbox Code Playgroud)
`
我用代码200(不应该是101)读取响应.
问题:
1.1.还能做些什么?
1.2.来自http的相同链接必须用于访问websocket吗?只用wss而不是https?
在此之后,我尝试将客户端连接到服务器并在onMessage方法中接收消息.
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container.connectToServer(ClientEndpoint.class, buildCEC(), new URI("wss://async.example.com/socket.io/1/?t=" + System.currentTimeMillis()));
我尝试在buildCec()中插入标题以连接到服务器.
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
Configurator conf = config.getConfigurator();
`Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Connection", …Run Code Online (Sandbox Code Playgroud)我需要访问HttpServletRequest属性以获取包含TLS 请求的证书数组的javax.servlet.request.X509CertificateX509Certificate。
从 JAX-RSContainerRequestFilter我可以轻松地从ContainerRequestContext.getProperty(String property)方法中提取它,但是我找不到从 WebSocketSession或 中获取它的方法HandshakeRequest,从中我可以访问HttpSession实例但不能访问实例HttpServletRequest。
注意:这不是Accessing HttpSession from HttpServletRequest in a Web Socket @ServerEndpoint因为我需要访问HttpServletRequest(或等效于提取 TLS 证书),而不是HttpSession.
由于 WebSocket 是 HTTP 的超集,我想它应该是可能的,希望 Java 团队已经想到了一种访问 servlet 属性的方法,但我真的找不到。任何人都知道这是否可能?
我想通过Web套接字向特定用户发送消息。到目前为止,我可以打开Web套接字并像这样从客户端读取消息:
@ServerEndpoint(value = "/wsep")
public class WebSocketEndpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEndpoint.class);
private Session session;
@OnOpen
public void onOpen(Session session) {
this.session = session;
try {
session.getBasicRemote().sendText("You are connected. Your ID is " + session.getId());
} catch (Exception e) {
LOGGER.error("Error on open web socket", e);
}
}
@OnMessage
public void onClientMessage(String message, Session session) {
LOGGER.info("Message from {} is: {}", session.getId(), message);
}
@OnClose
public void onClose(Session session) {
this.session = null;
LOGGER.info("{} disconnected", session.getId());
} …Run Code Online (Sandbox Code Playgroud)