WebSocket 会话中对象的处理
Handling of Objects in WebSocket Session
我正在尝试在 Java 中构建一个 WebSocket 服务器,并与一个简单的 javascript WebSocketClient 连接。
现在我想 "give" 每个传入的会话都是一个对象的唯一实例,该对象在 onopen 上创建,它们的方法在 onmessage 上调用并在 onclose 上销毁。我如何管理这些对象?有没有办法将它存储在会话中?或者服务器本身是否处理这些事情,我可以在 "Endpoint class"?
中定义一个私有变量
package com.xxx.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.glassfish.tyrus.server.Server;
public class TestWebSocketServer {
public static void main(String[] args) {
runServer();
}
public static void runServer() {
Server server = new Server("localhost", 21212, "/xxx", TestServerEndpoint.class);
try {
server.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please press a key to stop the server.");
reader.readLine();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
server.stop();
}
}
}
package com.xxx.server;
import java.io.IOException;
import java.util.logging.Logger;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/TEST")
public class TestServerEndpoint {
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
logger.info("Connected ... " + session.getId());
}
@OnMessage
public String onMessage(String message, Session session) {
switch (message) {
case "quit":
try {
session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE, "Test ended"));
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
return message;
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
}
}
您不需要自己处理这些对象,Java EE 服务器会处理它。您只需要在端点 class 中创建一个实例变量。例如,
@ServerEndpoint(value = "/test-server/")
public class TestWebSocketServer {
private YourObject yourObject = new YourObject();
//or instantiate it in constructor
}
只要您打开与服务器的新 websocket 连接,JavaEE 服务器就会创建一个新的 yourObject,并会在您关闭连接时为垃圾收集做好准备。
我正在尝试在 Java 中构建一个 WebSocket 服务器,并与一个简单的 javascript WebSocketClient 连接。 现在我想 "give" 每个传入的会话都是一个对象的唯一实例,该对象在 onopen 上创建,它们的方法在 onmessage 上调用并在 onclose 上销毁。我如何管理这些对象?有没有办法将它存储在会话中?或者服务器本身是否处理这些事情,我可以在 "Endpoint class"?
中定义一个私有变量package com.xxx.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.glassfish.tyrus.server.Server;
public class TestWebSocketServer {
public static void main(String[] args) {
runServer();
}
public static void runServer() {
Server server = new Server("localhost", 21212, "/xxx", TestServerEndpoint.class);
try {
server.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please press a key to stop the server.");
reader.readLine();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
server.stop();
}
}
}
package com.xxx.server;
import java.io.IOException;
import java.util.logging.Logger;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/TEST")
public class TestServerEndpoint {
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
logger.info("Connected ... " + session.getId());
}
@OnMessage
public String onMessage(String message, Session session) {
switch (message) {
case "quit":
try {
session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE, "Test ended"));
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
return message;
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
}
}
您不需要自己处理这些对象,Java EE 服务器会处理它。您只需要在端点 class 中创建一个实例变量。例如,
@ServerEndpoint(value = "/test-server/")
public class TestWebSocketServer {
private YourObject yourObject = new YourObject();
//or instantiate it in constructor
}
只要您打开与服务器的新 websocket 连接,JavaEE 服务器就会创建一个新的 yourObject,并会在您关闭连接时为垃圾收集做好准备。