package springwebsocketclient;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
/**
*
* @author Anurag
*/
public class SimpleSpringWebSocketClient {
public static void main(String[] args) {
StandardWebSocketClient standardWebSocketClient = new StandardWebSocketClient();
WebSocketTransport socketTransport = new WebSocketTransport(standardWebSocketClient);
List<Transport> transports = new ArrayList<>();
transports.add(socketTransport);
SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new SockJsMessageCodec() {
@Override
public String encode(String... messages) {
System.out.println("inside encode");
return "";
}
@Override
public String[] decode(String content) throws IOException {
System.out.println("inside decode");
return new String[10];
}
@Override
public String[] decodeInputStream(InputStream content) throws IOException {
System.out.println("inside decodeInputStream");
return new String[10];
}
});
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
String host = "192.168.3.202";
int port = 8080;
String stompUrl = "ws://" + host + ":" + port + "/hello";
WebSocketHttpHeaders webSocketHttpHeaders = new WebSocketHttpHeaders(new HttpHeaders());
LocalStompSessionHandler handler = new LocalStompSessionHandler();
StompHeaders stompHeaders = new StompHeaders();
try {
//stompClient.connect(stompUrl, new LocalStompSessionHandler());
stompClient.connect(stompUrl, handler);
} catch (Exception e) {
e.printStackTrace();
}
stompClient.setAutoStartup(true);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package springwebsocketclient;
import java.lang.reflect.Type;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandler;
/**
*
* @author Anurag
*/
public class LocalStompSessionHandler implements StompSessionHandler{
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
System.out.println("connectedHeaders = " + connectedHeaders);
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
}
@Override
public Type getPayloadType(StompHeaders headers) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Server Implementation
package hello;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
System.out.println("control come here");
registry.addEndpoint("/hello").withSockJS();
}
package hello;
public class HelloMessage {
private String name;
public String getName() {
return name;
}
}
package hello;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
package hello;
public class Greeting {
private String content;
public Greeting(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当我尝试将客户端连接到服务器时获取此异常:
2016年5月18日下午6:14:15 org.springframework.web.socket.sockjs.client.DefaultTransportRequest $ ConnectCallback handleFailure 严重:在TransportRequest之后不再有后备传输[url = ws://192.168.3.202:8080 / hello / 80 / 6af5b9dd5e0c4fd0be3daee63b74af0b / websocket] javax.websocket.DeploymentException:握手错误。 在org.glassfish.tyrus.client.ClientManager $ 3 $ 1.run(ClientManager.java:636) 在org.glassfish.tyrus.client.ClientManager $ 3.run(ClientManager.java:673) at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在org.glassfish.tyrus.client.ClientManager $ SameThreadExecutorService.execute(ClientManager.java:826) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112) 在org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:496) 在org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:348) 在org.springframework.web.socket.client.standard.StandardWebSocketClient $ 1.call(StandardWebSocketClient.java:152) 在org.springframework.web.socket.client.standard.StandardWebSocketClient $ 1.call(StandardWebSocketClient.java:149) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.lang.Thread.run(Thread.java:745) 引起:org.glassfish.tyrus.core.HandshakeException:响应代码不是101:403。 在org.glassfish.tyrus.client.TyrusClientEngine.processResponse(TyrusClientEngine.java:419) 在org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleHandshake(GrizzlyClientFilter.java:343) 在org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleRead(GrizzlyClientFilter.java:313) 在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545) ......还有1个
API版本是:
Spring 4.2.5
apache-loggings 1.1.1
J2EE 7
J2SE 8
Jackson-all 1.9
jacksone-core 2.2.3
tyrus-standalone-client 1.12
答案 0 :(得分:1)
因为没有定义CORS原始头,所以它会抛出403错误。 当您启用Spring Framework Web套接字的调试日志时,它将显示以下错误:
OriginHandshakeInterceptor - 拒绝握手请求,Origin标头 值192.168.3.202:8080不允许
将setAllowedOrigins(“*”)或setAllowedOrigins(“http://192.168.3.202:8080”)添加到您的端点,它将正常工作。
if (RasterSet[i] >= 0 && Rasterset[i] =< 10)
RasterSet[i].Average(z=> z.Z);