[英]How to maintain state with GlassFish4 Socket Server?
I am building a game using a socket server via GlassFish
4 . 我正在通过
GlassFish
4使用套接字服务器构建游戏 。
The problem I'm running into is that, 我遇到的问题是,
every time a socket connection is made my @ServerEndpoint class is re-instantiated . 每次建立套接字连接时,我的@ServerEndpoint类都会重新实例化 。
I'm basically just looking for the class that kicks off the GlassFish
4 server similar to writing a Servlet in Tomcat
. 我基本上只是在寻找启动
GlassFish
4服务器的类,类似于在Tomcat
编写Servlet。 I just need a place to initialize the state of my game , I thought the server end-point would be that place, but it's re-initializing my game state every time I open a connection to it (which is obviously problematic). 我只需要一个位置来初始化游戏状态 ,我以为服务器端点就是那个位置,但是每次我打开与游戏状态的连接时,它都会重新初始化游戏状态 (这显然是有问题的)。
I could write my game state containers as singletons or static classes with static initializers , but I would like to avoid both of those things and instantiate all of my game state containers in, I guess the class that sits listening for the web socket connections ? 我可以将游戏状态容器编写为具有静态初始化程序的 单例或静态类 ,但是我想避免这两种情况,并在其中实例化所有游戏状态容器,我想这是侦听Web套接字连接的类吗?
I haven't been able to find something like that in the glassfish tutorials I have read through. 在我阅读的Glassfish教程中,我找不到类似的东西。
I have found the solution to this. 我已经找到解决方案。 I was not declaring my data structures with "static final".
我没有用“ static final”声明数据结构。 Doing just "private HashMap<> x;"
只做“私有HashMap <> x;” resulted in "x" being reinstantiated every time a new connection was opened.
导致在每次打开新连接时重新实例化“ x”。
"private static final HashMap<> x;" “私有静态最终HashMap <> x;” looks like it stopped it from doing this.
看起来它阻止了此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.