英文:
why the websocket client user session is null after connect to server
问题
以下是您提供的代码的翻译部分:
package com.sportswin.soa.robot.websocket.client;
import com.sportswin.soa.robot.common.SessionUtil;
import com.sportswin.soa.robot.model.entity.Robot;
import com.sportswin.soa.robot.model.enumn.RobotStatus;
import com.sportswin.soa.robot.service.impl.RobotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.URI;
/**
* Websocket客户端连接器
*/
@Slf4j
@Component
public class WebsocketClientConnector {
@Value("${com.dabai.robot.ws-url}")
private String websocketUrl;
@Autowired
private SessionUtil sessionUtil;
public synchronized WebsocketClientEndpoint robotNewConnect(Long roomTypeId, String token, String userMark) {
WebsocketClientEndpoint clientEndPoint = null;
String websocketConnUrl = websocketUrl + "?token=" + token + "&roomTypeId=" + roomTypeId + "&robotFlag=1";
try {
String appMark = SessionUtil.getThreadLocal("appMark");
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
if (clientEndPoint == null) {
return null;
}
clientEndPoint.userSession.getUserProperties().put("userIdentity", userMark + "-" + appMark + "-" + roomTypeId);
clientEndPoint.addMessageHandler(message -> {
log.info("addMessageHandler:", message);
});
} catch (Exception e) {
log.error("Websocket error", e);
}
return clientEndPoint;
}
}
出现问题的地方:
javax.websocket.DeploymentException: 启动WebSocket连接的HTTP请求失败
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:488) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
// ... 其他堆栈信息 ...
Caused by: java.util.concurrent.TimeoutException: null
at org.apache.tomcat.websocket.AsyncChannelWrapperSecure$WrapperFuture.get(AsyncChannelWrapperSecure.java:508) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:346) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
... 30 common frames omitted
请注意,由于您已经明确表示只需要翻译代码部分,我将不会提供关于您问题的回答。如果您对问题有进一步的疑问,请随时提问。
英文:
I am using this code to connect to websocket with Java 8 in spring boot project:
package com. sportswin.soa.robot.websocket.client;
import com. sportswin.soa.robot.common.SessionUtil;
import com. sportswin.soa.robot.model.entity.Robot;
import com. sportswin.soa.robot.model.enumn.RobotStatus;
import com. sportswin.soa.robot.service.impl.RobotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.URI;
/**
* @author dolphin
*/
@Slf4j
@Component
public class WebsocketClientConnector {
@Value("${com.dabai.robot.ws-url}")
private String websocketUrl;
@Autowired
private SessionUtil sessionUtil;
public synchronized WebsocketClientEndpoint robotNewConnect(Long roomTypeId, String token, String userMark) {
WebsocketClientEndpoint clientEndPoint = null;
String websocketConnUrl = websocketUrl + "?token=" + token + "&roomTypeId=" + roomTypeId + "&robotFlag=1";
try {
String appMark = SessionUtil.getThreadLocal("appMark");
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
if (clientEndPoint == null) {
return null;
}
clientEndPoint.userSession.getUserProperties().put("userIdentity", userMark + "-" + appMark + "-" + roomTypeId);
clientEndPoint.addMessageHandler(message -> {
log.info("addMessageHandler:", message);
});
} catch (Exception e) {
log.error("Websocket error", e);
}
return clientEndPoint;
}
}
when run to get clientEndPoint.userSession
, the result is null. Why the user session is null when establish connnections? what should I do to make sure the user session exists? When I paste the websocket connection url in webbroswer plugin, it could connect successfully.
and the eror log output like this:
javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:488) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:197) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:154) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at com.sportswin.soa.robot.websocket.client.WebsocketClientEndpoint.<init>(WebsocketClientEndpoint.java:53) ~[classes!/:na]
at com.sportswin.soa.robot.websocket.client.WebsocketClientConnector.robotNewConnect(WebsocketClientConnector.java:35) [classes!/:na]
at com.sportswin.soa.robot.websocket.client.WebsocketClientUtil.websocketReconnection(WebsocketClientUtil.java:127) [classes!/:na]
at com.sportswin.soa.robot.websocket.client.WebsocketClientUtil.createWebsocketHandle(WebsocketClientUtil.java:85) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.allowRobotSelfPlay(PlayEntryOperate.java:290) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.addRobotBatch(PlayEntryOperate.java:213) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.handleCommonGame(PlayEntryOperate.java:189) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.lambda$matchByRoomType$4(PlayEntryOperate.java:142) [classes!/:na]
at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_212]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.matchByRoomType(PlayEntryOperate.java:135) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.allRoomRobotBalance(PlayEntryOperate.java:105) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.playRoomEntry(PlayEntryOperate.java:72) [classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.matchTenant(EnvelopRobotJobHandler.java:60) ~[classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.jobExecutor(EnvelopRobotJobHandler.java:48) ~[classes!/:na]
at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.runJob(EnvelopRobotJobHandler.java:40) ~[classes!/:na]
at sun.reflect.GeneratedMethodAccessor459.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_212]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_212]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_212]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_212]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_212]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_212]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_212]
Caused by: java.util.concurrent.TimeoutException: null
at org.apache.tomcat.websocket.AsyncChannelWrapperSecure$WrapperFuture.get(AsyncChannelWrapperSecure.java:508) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:346) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
... 30 common frames omitted
答案1
得分: 1
你从这里收到一个超时异常:
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
你已经正确填写了变量 com.dabai.robot.ws-url 吗?
我猜想变量没有被正确填充,或者在 URL 中的主机和端口不可达。
英文:
You get a timeout exception from here:
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
You have filled the variable com.dabai.robot.ws-url propperly?
I suppose that he variable not filled properly or the host and port in the url is not reachable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论