我在Spring mvc中创建了一个名为/ rtisTrackDataGIS的JSR 356 Web套接字 并希望将其连接到require js(前端ip = 192.168.101.158) 并且服务器IP为192.168.113.157,因此基本上我是通过交叉原点和
连接到websocket的 $ clang-query-7 ast.cpp --
clang-query> m callExpr(callee(functionDecl(hasName("somefunc"))))
Match #1:
/home/nicholas/ast.cpp:4:3: note: "root" binds here
somefunc(1 + somefunc(0));
^~~~~~~~~~~~~~~~~~~~~~~~~
Match #2:
/home/nicholas/ast.cpp:4:16: note: "root" binds here
somefunc(1 + somefunc(0));
^~~~~~~~~~~
2 matches.
并且我还包括一个corsfilter并添加了服务器作为跨源服务器,这与其余api控制器可以正常工作,但不能连接我创建的websocket
@ServerEndpoint( value="/rtisTrackDataGIS", configurator = SpringConfigurator.class, encoders = {WebSocketDataEncoder.class} )
public class SendTrainsInfoToClients {
UtilService utilService;
static ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
private static Set<Session> allSessions= Collections.synchronizedSet(new HashSet<Session>());
@Autowired
public void SendTrainsInfoToClients(UtilService utilService){
this.utilService = utilService;
}
@OnOpen
public void showRTISGISData(Session session){
allSessions.add(session);
// start the scheduler on the very first connection
// to call sendRTISGISDataToAll every second
if(allSessions.size() == 1){
System.out.println("Ok WebSocket opened - session id : " + session.getId());
timer.scheduleAtFixedRate(()->sendRTISGISDataToAll(), 0, 120, TimeUnit.SECONDS);
}
}
@OnClose
public void onClose(Session session){
System.out.println("WebSocket Closed - session id : " + session.getId());
allSessions.remove(session);
}
private void sendRTISGISDataToAll(){
String data = null;
if(allSessions.size() >= 1){
data ="sadasdad";
}
//System.out.println(data);
for(Session sess: allSessions){
try{
//System.out.println("Sending Trains Track data to client with session id "+sess.getId());
sess.getBasicRemote().sendText(data);
//sess.getBasicRemote().sendObject(jsonObject);;
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
} catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
public static void sendGISDataToAll(String message){
String data = null;
if(allSessions.size() >= 1){
data ="sadasdad";
}
//System.out.println(data);
for(Session sess: allSessions){
try{
//System.out.println("Sending Trains Track data to client with session id "+sess.getId());
sess.getBasicRemote().sendText(message);
//sess.getBasicRemote().sendObject(jsonObject);;
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
} catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}
并将websocket从192.168.101.158连接到192.168.113.157
public class CORSFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
System.out.println(
"Request Character: " + req.getRemoteAddr() + "--" + ((HttpServletRequest)req).getRequestURL() + "--" + ((HttpServletRequest) req).getMethod());
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request=(HttpServletRequest)req;
// System.out.println("Response Character: ");
response.setHeader("Access-Control-Allow-Origin","https://192.168.101.158:8443");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");//
response.setHeader("Access-Control-Max-Age", "3600");//prevous was 3600
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"); //
response.setHeader("Access-Control-Allow-Credentials", "true");
if("OPTIONS".equalsIgnoreCase(request.getMethod()))
{
response.setStatus(HttpServletResponse.SC_CREATED);
}
else
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
然后我无法连接它,我可以在@ServerEndpoint中指定任何cors =