如何在 java serverendpoint 的 websocket url 识别数据
how to identify data at the websocket url in java serverendpoint
ws://host:port/cms/ocpp/CBNO7
这是我的第一个websocket程序,这里url定义"cms"是projectname "ocpp"是serverendpoint最后一个是每个客户端端点的数据变化user.How 获取server中最后的数据endpoint.My java serverendpoint代码如下,
`import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.ws.rs.PathParam;
@ServerEndpoint("/ocpp")
public class OcppWebsocketServer {
@OnOpen
public void onOpen(Session session) throws IOException {
System.out.println(session.getId() + " has opened a connection");
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
}
@OnError
public void onError(Throwable error) {
System.out.println("error = " + error);
Logger.getLogger(OcppWebsocketServer.class.getName()).log(Level.SEVERE, null, error);
}
@OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
}
}`
如何在端点获得 CBNO7
您需要使用 PathParam
: http://docs.oracle.com/javaee/7/api/javax/websocket/server/PathParam.html
你最终会得到类似
的东西
@ServerEndpoint("/cms/ocpp/{parameter}")
public class OcppWebsocketServer{
@OnMessage
public void onMessage(@PathParam("parameter") String param, String message, Session session) {
// it'll print CBN07
System.out.println(param);
}
}
编辑
确保导入 javax.websocket.server.PathParam
而不是 JAX-RS one
ws://host:port/cms/ocpp/CBNO7
这是我的第一个websocket程序,这里url定义"cms"是projectname "ocpp"是serverendpoint最后一个是每个客户端端点的数据变化user.How 获取server中最后的数据endpoint.My java serverendpoint代码如下,
`import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.ws.rs.PathParam;
@ServerEndpoint("/ocpp")
public class OcppWebsocketServer {
@OnOpen
public void onOpen(Session session) throws IOException {
System.out.println(session.getId() + " has opened a connection");
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
}
@OnError
public void onError(Throwable error) {
System.out.println("error = " + error);
Logger.getLogger(OcppWebsocketServer.class.getName()).log(Level.SEVERE, null, error);
}
@OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
}
}`
如何在端点获得 CBNO7
您需要使用 PathParam
: http://docs.oracle.com/javaee/7/api/javax/websocket/server/PathParam.html
你最终会得到类似
的东西@ServerEndpoint("/cms/ocpp/{parameter}")
public class OcppWebsocketServer{
@OnMessage
public void onMessage(@PathParam("parameter") String param, String message, Session session) {
// it'll print CBN07
System.out.println(param);
}
}
编辑
确保导入 javax.websocket.server.PathParam
而不是 JAX-RS one