Spring4.3.3 WebSocket-STOMP协议集成 (2.1)-WebSocket-stomp子协议通讯小栗子

                      上一篇中说到:stomp - jsp之间的通讯,是通过stomp,socket.js/stomp.js实现的通讯。如果觉得写jsp或者js麻烦,或者不怎么会js,不是太懂,可以用webSocketStomp的client来测试。下面看代码:

                 pom.xml依赖:

	
	
    	org.glassfish.tyrus.bundles
    	tyrus-standalone-client
    	1.13
	

              JunitTest方法如下:具体和WebSocket的junit相似,不多解释了,老衲告退....

	/**
	 * 连接js endpoint端点
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
//	@Test
	public void testSocketJsEndpoint() throws InterruptedException, ExecutionException {
		//js url
		String jsUrl = "ws://localhost:18080/JeeSite/hello";
		//for js endpoint wrap
		List transports = Lists.newArrayList();
		transports.add(new WebSocketTransport(new StandardWebSocketClient()));
		WebSocketClient transport = new SockJsClient(transports);
		//js endpoint end
		
		WebSocketStompClient stompClient  = new WebSocketStompClient(transport);
		
		stompClient.setMessageConverter(new StringMessageConverter());
		
		//stompsession 使用必须配置Receiptable
		ThreadPoolTaskScheduler task = new ThreadPoolTaskScheduler();
		task.initialize();
		
		stompClient.setTaskScheduler(task);
		
		StompSessionHandler sessionHandler = new MyStompSessionHandler();
		
		ListenableFuture ret = stompClient.connect(jsUrl, sessionHandler);
		
		ret.addCallback(new ListenableFutureCallback() {
			
			@Override
			public void onSuccess(StompSession session) {
				session.subscribe("/app/subscribeme", new StompFrameHandler() {
					
					@Override
					public void handleFrame(StompHeaders headers, Object payload) {
						System.out.println(payload);
					}
					
					@Override
					public Type getPayloadType(StompHeaders headers) {
						return String.class;
					}
				});
				session.subscribe("/topic/hi", new StompFrameHandler() {
					
					@Override
					public void handleFrame(StompHeaders headers, Object payload) {
						System.out.println("subscribe message : ");
						System.out.println(payload);
					}
					
					@Override
					public Type getPayloadType(StompHeaders headers) {
						return Object.class;
					}
				});
			}

			@Override
			public void onFailure(Throwable ex) {
				ex.printStackTrace();
			}
		});
		
		StompSession sess = ret.get();
		//跟踪发送消息返回状态,必须启用该项
		sess.setAutoReceipt(true);
		
		Receiptable recpt = sess.send("/app/hi", "i'm js monkey stanthem.");
		
		
		recpt.addReceiptTask(new Runnable() {

			@Override
			public void run() {
				System.out.println("js endpoint send msg success.");
			} 
			
		});
		
	}
	
	/**
	 * java stomp client 连接client 端点。
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
//	@Test
	public void testStompClientEndpoint() throws InterruptedException, ExecutionException {
		String clientUrl = "ws://localhost:18080/JeeSite/client";
		
		WebSocketClient client = new StandardWebSocketClient();
		
		WebSocketStompClient stompClient = new WebSocketStompClient(client);
		
		stompClient.setMessageConverter(new StringMessageConverter());
		
		stompClient.setReceiptTimeLimit(300);
		//配置心跳频率,默认就是下面这个间隔
		stompClient.setDefaultHeartbeat(new long[]{10000l,10000l});
		
		//stompsession 使用必须配置Receiptable, taskScheduler作用是
		//Configure a scheduler to use for heartbeats and for receipt tracking.
		//为配置心跳频率和跟踪发送状态 准备的线程池
		ThreadPoolTaskScheduler task = new ThreadPoolTaskScheduler();
		task.initialize();
		
		stompClient.setTaskScheduler(task);
		
		
		StompSessionHandler sessionHandler = new MyStompSessionHandler();
		

		
		ListenableFuture ret = stompClient.connect(clientUrl, sessionHandler);
		ret.addCallback(new ListenableFutureCallback() {

			@Override
			public void onSuccess(StompSession session) {
				session.subscribe("/app/subscribeme", new StompFrameHandler() {
					
					@Override
					public void handleFrame(StompHeaders headers, Object payload) {
						System.out.println("subscribe message : ");
						System.out.println(payload);
					}
					
					@Override
					public Type getPayloadType(StompHeaders headers) {
						return String.class;
					}
				});
				session.subscribe("/topic/hi", new StompFrameHandler() {
					
					@Override
					public void handleFrame(StompHeaders headers, Object payload) {
						System.out.println(payload);
					}
					
					@Override
					public Type getPayloadType(StompHeaders headers) {
						return Object.class;
					}
				});
			}

			@Override
			public void onFailure(Throwable ex) {
				ex.printStackTrace();
			}
		});
		StompSession sess = ret.get();
		//跟踪发送消息返回状态,必须启用该项
		sess.setAutoReceipt(true);
		//发一个消息
		Receiptable recpt = sess.send("/app/hi", "i'm java stomp client monkey stanthem.");
		
		recpt.addReceiptTask(new Runnable() {

			@Override
			public void run() {
				System.out.println("java client endpoint send msg success.");
			} 
			
		});
	}

class MyStompSessionHandler extends StompSessionHandlerAdapter {
	@Override
	public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
		Map headers = connectedHeaders.toSingleValueMap();
		for (Entry kv : headers.entrySet()) {
			System.out.println(kv.getKey() + " - " + kv.getValue());
		}
		System.out.println("connected session : "+session.getSessionId());
	}
}


你可能感兴趣的:(Spring)