WebSocket跨域问题可以通过在服务端进行配置来解决。具体方法如下:
1.在服务端配置允许跨域访问的信息,例如在Spring Boot中可以使用@CrossOrigin注解或者配置CorsFilter来实现。
2.在客户端建立WebSocket连接时,需要在请求头中添加Origin字段,指定允许跨域的源地址。
3.在服务端接收到WebSocket连接请求时,需要在响应头中添加Access-Control-Allow-Origin字段,指定允许跨域的源地址。
下面是一个使用Spring Boot解决WebSocket跨域问题的例子:
```java
// 1.在WebSocket配置类中添加@CrossOrigin注解
@Configuration
@EnableWebSocket
@CrossOrigin(origins = "http://localhost:8080")
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}
// 2.在客户端建立WebSocket连接时添加Origin字段
var socket = new WebSocket("ws://localhost:8080/myHandler");
socket.onopen = function(event) {
socket.send("Hello, Server!");
};
// 3.在服务端响应头中添加Access-Control-Allow-Origin字段
public class MyHandler implements WebSocketHandler {
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
session.sendMessage(new TextMessage("Hello, Client!"));
session.getAttributes().put("username", "guest");
session.getAttributes().put("uid", "123456");
session.getAttributes().put("timestamp", System.currentTimeMillis());
session.getAttributes().put("ip", session.getRemoteAddress().getHostName());
session.getAttributes().put("port", session.getRemoteAddress().getPort());
session.getAttributes().put("protocol", session.getHandshakeHeaders().get("Sec-WebSocket-Protocol").get(0));
session.getAttributes().put("origin", session.getHandshakeHeaders().get("Origin").get(0));
session.getAttributes().put("user-agent", session.getHandshakeHeaders().get("User-Agent").get(0));
session.getAttributes().put("referer", session.getHandshakeHeaders().get("Referer").get(0));
session.getAttributes().put("cookie", session.getHandshakeHeaders().get("Cookie").get(0));
session.getAttributes().put("path", session.getUri().getPath());
session.getAttributes().put("query", session.getUri().getQuery());
session.getAttributes().put("params", session.getUri().getQueryParams());
session.getAttributes().put("headers", session.getHandshakeHeaders());
session.getAttributes().put("attributes", session.getAttributes());
session.getAttributes().put("principal", session.getPrincipal());
session.getAttributes().put("remote-address", session.getRemoteAddress());
session.getAttributes().put("local-address", session.getLocalAddress());
session.getAttributes().put("binary-message-size-limit", session.getBinaryMessageSizeLimit());
session.getAttributes().put("text-message-size-limit", session.getTextMessageSizeLimit());
session.getAttributes().put("idle-timeout", session.getIdleTimeout());
session.getAttributes().put("uri-variables", session.getUriVariables());
session.getAttributes().put("handshake-timeout", session.getHandshakeTimeout());
session.getAttributes().put("heartbeat-interval", session.getHeartbeatInterval());
session.getAttributes().put("send-timeout", session.getSendTimeout());
session.getAttributes().put("message-size-limit", session.getMessageSizeLimit());
session.getAttributes().put("extensions", session.getExtensions());
session.getAttributes().put("subprotocols", session.getAcceptedProtocol());
session.getAttributes().put("ssl-session", session.getSslSession());
session.getAttributes().put("local-name", session.getLocalAddress().getHostName());
session.getAttributes().put("local-port", session.getLocalAddress().getPort());
session.getAttributes().put("remote-name", session.getRemoteAddress().getHostName());
session.getAttributes().put("remote-port", session.getRemoteAddress().getPort());
session.getAttributes().put("remote-user", session.getPrincipal().getName());
session.getAttributes().put("remote-ip", session.getRemoteAddress().getAddress().getHostAddress());
session.getAttributes().put("remote-host", session.getRemoteAddress().getHostName());
session.getAttributes().put("remote-uri", session.getUri().toString());
session.getAttributes().put("remote-headers", session.getHandshakeHeaders());
session.getAttributes().put("remote-attributes", session.getAttributes());
session.getAttributes().put("remote-protocol", session.getHandshakeHeaders().get("Sec-WebSocket-Protocol").get(0));
session.getAttributes().put("remote-origin", session.getHandshakeHeaders().get("Origin").get(0));
session.getAttributes().put("remote-user-agent", session.getHandshakeHeaders().get("User-Agent").get(0));
session.getAttributes().put("remote-referer", session.getHandshakeHeaders().get("Referer").get(0));
session.getAttributes().put("remote-cookie", session.getHandshakeHeaders().get("Cookie").get(0));
session.getAttributes().put("remote-path", session.getUri().getPath());
session.getAttributes().put("remote-query", session.getUri().getQuery());
session.getAttributes().put("remote-params", session.getUri().getQueryParams());
session.getAttributes().put("remote-extensions", session.getExtensions());
session.getAttributes().put("remote-subprotocols", session.getAcceptedProtocol());
session.getAttributes().put("remote-ssl-session", session.getSslSession());
session.getAttributes().put("remote-local-name", session.getLocalAddress().getHostName());
session.getAttributes().put("remote-local-port", session.getLocalAddress().getPort());
session.getAttributes().put("remote-remote-name", session.getRemoteAddress().getHostName());
session.getAttributes().put("remote-remote-port", session.getRemoteAddress().getPort());
session.getAttributes().put("remote-remote-user", session.getPrincipal().getName());
session.getAttributes().put("remote-remote-ip", session.getRemoteAddress().getAddress().getHostAddress());
session.getAttributes().put("remote-remote-host", session.getRemoteAddress().getHostName());
session.getAttributes().put("remote-remote-uri", session.getUri().toString());
session.getAttributes().put("remote-remote-headers", session.getHandshakeHeaders());
session.getAttributes().put("remote-remote-attributes", session.getAttributes());
session.sendMessage(new TextMessage("Hello, Client!"));
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
session.sendMessage(new TextMessage("Hello, Client!"));
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
session.sendMessage(new TextMessage("Hello, Client!"));
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
session.sendMessage(new TextMessage("Hello, Client!"));
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}
```